5 python tkinter widget tkinter-entry
我一直在编写一个 tkinter 应用程序,它从多个条目小部件中获取浮点数,然后在计算中使用浮点数来生成一个浮点数,然后可以显示在适当的标签中。
当我运行应用程序时,我得到“ValueError: could not convert string to float:”并且我不能在任何计算中使用浮点数。
这是一小段代码:
def click_p1():
p1 = entry_p1.get()
try:
p1 = float(p1)
print (p1)
except:
print('Bad Input')
button_p1 = Button(text = 'ENTER', command = click_p1).grid(row = 2, column = 1, stick = 'nsew')
entry_p1 = Entry()
entry_p1.grid(row = 2, column = 0, stick = 'nsew')
p1 = float(entry_p1.get())
p2 = float(entry_p2.get())
t1 = float(entry_t1.get())
t2 = float(entry_t2.get())
a1 = math.log(p1) - math.log(p2)
b1 = t2 - t1
k1 = a1/b1
e2hours = -k1*(120 + t1)
p2hours += p1*math.exp(e2hours)
print(p2hours)
Run Code Online (Sandbox Code Playgroud)
我怎样才能让程序接受输入的数字是一个浮点数,然后在计算中使用它?
如果答案很明显,我很抱歉,我对编程和使用 tkinter 还很陌生。
小智 3
“ValueError:”表示您输入的值不被理解。当您输入一个数字时,您知道它是一个数字,但计算机将其作为字符串接收。尝试:
p1 = float(entry_p1.text())
Run Code Online (Sandbox Code Playgroud)
这是我犯过很多次的错误。继续编码。
| 归档时间: |
|
| 查看次数: |
13780 次 |
| 最近记录: |