[英]How to fix “ValueError: could not convert string to float:” in Python with tkinter
I'm using tkinter interface with Python 3.7.2, and it gives out: 我在Python 3.7.2中使用了tkinter接口,它给出了:
ValueError: could not convert string to float:
ValueError:无法将字符串转换为float:
I need to take content from Edit's
and multiply them by showing them on Label
. 我需要从
Edit's
内容中提取内容,并通过在Label
上显示它们来使其倍增。
I tried using int
but I'm using number '2.5' 我尝试使用
int
但使用的是数字“ 2.5”
def count():
x = float(EditBox1_text.get())
y = float(EditBox2_text.get())
return x * y
EditBox1_text = StringVar(0)
EditBox2_text = StringVar(0)
EditBox1 = Entry(MainActivity, textvariable=EditBox1_text).grid(row=0, column=0, sticky=W)
EditBox2 = Entry(MainActivity, textvariable=EditBox2_text).grid(row=1, column=0, sticky=W)
I'm tried this, and it's worked: 我已经尝试过了,并且成功了:
def count():
x = float(EditBox1_text.get())
y = float(EditBox2_text.get())
AnswerText_text.set(x * y)
Before, i called 以前我打电话
AnswerText_text.set(count())
At run of code 在运行代码
Tkinter constructor of StringVar
takes no value argument, unless if pointed as a keyword argument, as pointed by @BryanOakley, So, you cannot initialize StringVar
with a value without the keyword argument declared. StringVar
Tkinter构造函数不接受任何值参数,除非由@BryanOakley指出,如果将其指向为关键字参数,则无法在未声明关键字参数的情况下使用值初始化StringVar
。 For that you have to use .set
, to set the variable value, as so: 为此,您必须使用
.set
来设置变量值,如下所示:
EditBox1_text = StringVar()
EditBox2_text = StringVar()
EditBox1_text.set("2.5")
EditBox2_text.set("3.8")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.