简体   繁体   English

无法使用 float.get() 从 tkinter 条目中检索浮点数。 我怎样才能解决这个问题?

[英]cant retrieve float from tkinter entry using float.get(). how can i fix this?

im trying to retrieve a value from an entry box and retrieve it as a float but it does not work.我试图从输入框中检索一个值并将其作为浮点数检索,但它不起作用。 i have a function that is supposed to retrieve the values from three entry boxes, when a button is pushed the value received are used in 3 different function depending on what has been chosen from a drop down list我有一个函数应该从三个输入框中检索值,当按下按钮时,接收到的值用于 3 个不同的函数,具体取决于从下拉列表中选择的内容

def selected():
    length = float(l_input.get())
    width = float(w_input.get())
    height = float(h_input.get())
    selected = var.get()
    if selected == "cube":
        cube
    elif selected == "cuboid":
        cuboid
    elif selected == "cylinder":
        cylinder

this is supposed to run when this button is pressed这应该在按下此按钮时运行

size_input = Button(window, text="Enter", command=selected()) 
size_input.place(x=470,y=50,width=50,height=25)

ive tried many times to fix this and either it dosent work or it refuses to run at all and gives me an error like AttributeError: 'NoneType' object has no attribute 'get' or it will give me the error ValueError: could not convert string to float: '' i honestly have no clue for to fix this plz help我尝试了很多次来解决这个问题,要么它不起作用,要么它根本拒绝运行,并给我一个错误,比如AttributeError: 'NoneType' object has no attribute 'get'或者它会给我错误ValueError: could not convert string浮动:''老实说,我不知道要解决这个问题请帮忙

l_lb = Label(window, text = "L:")
l_lb.place(x=180,y=50,width=25,height=25) 
l_input = Entry(window)
l_input.place(x=200,y=50,width=57,height=25)
w_lb = Label(window, text = "W:")
w_lb.place(x=280,y=50,width=25,height=25) 
w_input = Entry(window)
w_input.place(x=300,y=50,width=57,height=25)
h_lb = Label(window, text = "H")
h_lb.place(x=380,y=50,width=25,height=25) 
h_input = Entry(window)
h_input.place(x=400,y=50,width=57,height=25)

(above is code for entries ) (以上是条目代码)

First, the code you provided does not give errors of the type AttributeError: 'NoneType' object has no attribute 'get' but if you get some, see Tkinter: AttributeError: NoneType object has no attribute <attribute name> .首先,您提供的代码没有给出AttributeError: 'NoneType' object has no attribute 'get'类型的错误,但如果你得到一些,请参阅Tkinter: AttributeError: NoneType object has no attribute <attribute name>

Second, you get ValueError: could not convert string to float because the function selected() is executed when the button size_input is created and at that moment the entries are empty, therefore l_input.get() returns '' which then triggers an error when trying to convert this empty string to float.其次,您会得到ValueError: could not convert string to float因为在创建按钮size_input时执行了函数selected()并且此时条目为空,因此l_input.get()返回'' ,然后触发错误试图将此空字符串转换为浮点数。 To fix that, you need to pass the function to the command option of the button without brackets:要解决这个问题,您需要将函数传递给不带括号的按钮的command选项:

size_input = Button(window, text="Enter", command=selected) 

Side comment: I don't recommend using .place() to display all the widgets because you have to make sure manually that they don't overlap and even then they do not adjust when the window is resized.旁注:我不建议使用.place()来显示所有小部件,因为您必须手动确保它们不会重叠,即使在调整窗口大小时它们也不会调整。 On the contrary, .grid() and .pack() manage automatically this kind of things相反, .grid().pack()自动管理这类事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM