【发布时间】:2015-03-01 13:50:45
【问题描述】:
我一直在编写一个 tkinter 应用程序,它从多个条目小部件中获取浮点数,然后在计算中使用浮点数来生成一个浮点数,然后可以在适当的标签中显示该浮点数。
当我运行应用程序时,我得到“ValueError:无法将字符串转换为浮点数:”并且我无法在任何计算中使用浮点数。
这是一小段代码:
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)
如何让程序接受输入的数字是浮点数,然后在计算中使用它?
如果答案很明显,我很抱歉,我对编程和使用 tkinter 还是很陌生。
【问题讨论】:
-
忘了提到 p1、p2、t1 和 t2 有条目小部件和其他按钮,但我没有包括所有这些以使问题更简洁。以防万一不是很明显。
标签: python tkinter widget tkinter-entry