Tkinter 入口小部件浮动 [英] Tkinter entry widget float
问题描述
我一直在编写一个 tkinter 应用程序,它从多个条目小部件中获取浮点数,然后在计算中使用浮点数来生成一个浮点数,然后可以显示在适当的标签中.
I have been writing a tkinter application that takes floats from multiple entry widgets and then uses the floats in a calculation to produce a float that can then be displayed in an appropriate label.
当我运行应用程序时,我收到ValueError:无法将字符串转换为浮点数:"并且我无法在任何计算中使用浮点数.
When I run the application I get 'ValueError: could not convert string to float:' and I can't use the floats in any calculations.
这是一小段代码:
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)
我怎样才能让程序接受输入的数字是一个浮点数,然后在计算中使用它?
How could I get the program to accept the number entered is a float and then use it in the calculation?
如果答案很明显,我很抱歉,我对编程和使用 tkinter 还很陌生.
I'm sorry if the answer is obvious, I'm quite new to programming and using tkinter.
推荐答案
"ValueError:" 表示您输入的值不被理解.当您输入一个数字时,您知道它是一个数字,但计算机将其作为字符串接收.试试:
"ValueError:" means that the value you entering is not understood. When you enter a number you know it is a number but the computer takes it in as string. try:
p1 = float(entry_p1.text())
我犯了很多错误.继续编码.
It is a mistake I have made a lot. Keep coding.
这篇关于Tkinter 入口小部件浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!