我有一个主要的PyQt窗口,当我们点击某个按钮时,我需要从中获取一串用户输入.
这是我的用户输入窗口的代码:
class InputDialog(QtGui.QDialog):
'''
this is for when you need to get some user input text
'''
def __init__(self, parent=None, title='user input', label='comment', text=''):
QtGui.QWidget.__init__(self, parent)
#--Layout Stuff---------------------------#
mainLayout = QtGui.QVBoxLayout()
layout = QtGui.QHBoxLayout()
self.label = QtGui.QLabel()
self.label.setText(label)
layout.addWidget(self.label)
self.text = QtGui.QLineEdit(text)
layout.addWidget(self.text)
mainLayout.addLayout(layout)
#--The Button------------------------------#
layout = QtGui.QHBoxLayout()
button = QtGui.QPushButton("okay") #string or icon
self.connect(button, QtCore.SIGNAL("clicked()"), self.close)
layout.addWidget(button)
mainLayout.addLayout(layout)
self.setLayout(mainLayout)
self.resize(400, 60)
self.setWindowTitle(title)
Run Code Online (Sandbox Code Playgroud)
从主窗口,我说:
inputter = InputDialog(mainWindowUI, title="comments", label="comments", text="")
inputter.show()
comment = inputter.text.text()
print comment
Run Code Online (Sandbox Code Playgroud)
即使用户键入注释并点击"确定",也会打印一个空字符串.显然因为主窗口脚本不等待InputDialog …