简体   繁体   English

将QLineEdit文本转换为浮点-PySide / pyQt4

[英]Converting QLineEdit text to float - PySide/pyQt4

How do I convert a number in a LineEdit widget to float? 如何将LineEdit小部件中的数字转换为浮点型?

Say I have: 说我有:

textfield = QtGui.QLineEdit(self)

And the user inputs 12345.2 in it. 用户在其中输入12345.2。 I want that to be converted to a float and stored in a variable X. How do I do it? 我希望将其转换为浮点数并存储在变量X中。我该怎么做?

I tried the code below, it doesn't work. 我尝试了下面的代码,它不起作用。

INPUT = textfield.text
INPUT2 = float(INPUT)

text is the name of the method. text是方法的名称。 You should call it to get the text inside the QLineEdit . 您应该调用它以获取QLineEdit的文本。

INPUT = textfield.text()
INPUT2 = float(INPUT)

Try: INPUT2 = float(INPUT) 尝试:INPUT2 = float(INPUT)

You have INPUT2 where you should have INPUT 您有INPUT2,应在其中输入INPUT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM