【问题标题】:How do I convert Entry String into int or float?如何将 Entry String 转换为 int 或 float?
【发布时间】:2019-12-11 16:51:12
【问题描述】:

如何将 Entry 字符串转换为数字 int 或 float 值

#========================= Imports files and Widgets 
#===================================================================

from tkinter import *

#========================= Main Window Size 
main_window = Tk()
main_window.title("Entry Input Test")

width = 400
height = 200
main_window.geometry(str(width) + "x" + str(height) + "+600+520")
main_frame=Frame(main_window)

################ Varibles 

VarWidthECC = 20

################ Input Boxes 

如何将 Entry 输入字符串转换为 int 或 float 值

First_Value=Label(main_frame,text="Enter First Value ")
First_Value.grid(row=10,column=5, padx=10, sticky=E)
First_Value=(Entry(main_frame, width=VarWidthECC, justify='right'))
#First_Value=Input(main_frame, width=VarWidthECC, justify='right')
First_Value.grid(row=10,column=6, padx=10, pady=0)

Second_Value=Label(main_frame,text="Enter Second Value ")
Second_Value.grid(row=20,column=5, padx=10, sticky=E)
Second_Value=Entry(main_frame, width=VarWidthECC, justify = 'right')
#Second_Value=Input(main_frame, width=VarWidthECC, justify = 'right')
Second_Value.grid(row=20,column=6, padx=20, pady=10)

################ Comparison Function 

def Enter_Button():

    #global First_Value, Second_Value

    print("First Value is: " + First_Value.get())
    print("Second Value is: " + Second_Value.get())
    #value = int(First_Value)
    print(type(First_Value))

如何将 Entry 字符串转换为数字 int 或 float 值

    #Multiply_test = int(First_Value) * 5  #this line of code is Error
    #How can I convert Entry string into int or float??

下面的乘法测试有效 Multiply_test = 6 * 6 print("Multiply_test 是:" + str(Multiply_test))

################ Run Button ####################################
button_enter = Button(main_frame, width=20, background="light grey", 
text=" Enter ", font="Verdana 10 bold", command=Enter_Button, bd=6, 
relief='raise')
button_enter.grid(row=100, column=6, padx=10, pady=5)

####################################################

main_frame.grid(row=0,column=0)       # Prints out Labels on Main Window
main_window.mainloop()                # Code to End Main Window (root)

【问题讨论】:

    标签: python tkinter tkinter-entry


    【解决方案1】:

    使用.get() 方法以字符串的形式给出条目的输入。您正在尝试将 tkinter 条目转换为 float/int,但您忘记使用 get 方法。

    用这个替换 Enter_Button() 并尝试:

    def Enter_Button():
    
        #global First_Value, Second_Value
    
        print("First Value is: " + First_Value.get())
        print("Second Value is: " + Second_Value.get())
        #value = int(First_Value)
        print(type(First_Value.get()))   #< Use get method as you used it above
        print(type(int(First_Value.get())))
        print(type(float(First_Value.get())))
    

    【讨论】:

    • 非常感谢,您的代码运行良好。 print(type(int(First_Value.get()))) print(type(float(First_Value.get())))
    【解决方案2】:

    .get() 返回一个str 对象,我们已经知道python 使用. 进行浮点识别。

    if '.' in First_Value.get():
        # You got an Float
        return float(First_Value.get()) # Never reached to else for TYPE error
    else :
        #You got an Integer
        return int(First_Value.get())
    

    其实这不是一个完全的灵魂乐,check this .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2016-03-11
      • 2011-11-02
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      相关资源
      最近更新 更多