[英]How to get input entered in QLineEdit by clicking QPushButton and then print result in QLabel?
[英]How to print input of QLineEdit/ store in variable?
如何打印QLineEdit
的输入? 还是将其存储为变量供以后在函数中使用?
这是我尝试过的:
self.QLineEdit.text()
我的完整代码真的很混乱,如果有人在某个地方有一个示例,我将非常感激。 我的代码未返回任何文本。
不确定您的意思是什么,但是这是一个在类中显示QLineEdit的示例,该示例显示如何返回值并存储它。
from PyQt import QtGui
class SimpleExample(object):
def __init__(self):
# instance line edit
self.line_edit = QtGui.QLineEdit()
def pretend_something_happened(self):
# User Did something
self.line_edit.setText("User Entered Something")
def line_value(self):
# return text value of line edit
return self.line_edit.text()
if __name__ == '__main__':
s = SimpleExample()
# Store Value
val = s.line_value()
print(val)
s.pretend_something_happened()
# Print Value to show the value was copied
print(val)
# Store new value
val = s.line_value()
print(val)
如@Andrew Paxson的答案中所述,您不能使用self.QLineEdit
。 您的窗口中必须有一个实例:
self.line_edit = QtGui.QLineEdit()
那么您可以使用self.line_edit.text
来存储或打印其值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.