简体   繁体   English

如何使用tkinter修复Python中的“ ValueError:无法将字符串转换为float:”

[英]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.

相关问题 如何修复 ValueError :无法将字符串转换为浮点数:在 Python 中 - how to fix ValueError :could not convert string to float: in Python 如何修复“ValueError:无法将字符串转换为浮点数:'East'”(Python) - How to fix "ValueError: could not convert string to float: 'East'" (Python) 如何修复 ValueError: could not convert string to float: in python - How to fix the ValueError: could not convert string to float: in python 如何修复“ValueError:无法将字符串转换为浮点数”? - How to fix “ValueError: could not convert string to float”? 如何修复“ValueError:无法将字符串转换为浮点数” - How to fix “ValueError: could not convert string to float” ValueError:无法在tkinter中将字符串转换为float: - ValueError: could not convert string to float: in tkinter tkinter:如何避免这个`ValueError:无法将字符串转换为浮点数:'? - tkinter: How to avoid this `ValueError: could not convert string to float:'? 如何修复此错误:ValueError:无法将字符串转换为浮点数:'A' - How to fix this error: ValueError: could not convert string to float: 'A' 如何修复错误 ValueError: could not convert string to float in a NLP project in python? - how to fix the error ValueError: could not convert string to float in a NLP project in python? ValueError:无法将字符串转换为浮点数:'F' python - ValueError: could not convert string to float: 'F' python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM