从主 PyQt 窗口启动 PyQT 窗口,并获取用户输入?
我有一个 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)
在主窗口中,我说:
inputter = InputDialog(mainWindowUI, title="comments", label="comments", text="")
inputter.show()
comment = inputter.text.text()
print comment
即使用户键入注释并点击“确定”,这也会打印一个空字符串。显然是因为主窗口脚本不会等待 InputDialog
关闭。那么,我如何让它等待,以便我可以检索用户输入?
I have a main PyQt window, from which I need to get a string of User Input when they hit a certain button.
Here is my code for the User Input window:
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)
From the main window, I am saying:
inputter = InputDialog(mainWindowUI, title="comments", label="comments", text="")
inputter.show()
comment = inputter.text.text()
print comment
This prints an empty string, even if the user types a comment and hits "OK". Obviously because the main window script does not wait on the InputDialog
to close. So, how do I get it to wait, so that I may retrieve the user input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
而不是
来自:http://www.riverbankcomputing。 co.uk/static/Docs/PyQt4/html/qdialog.html#exec
Use
instead of
From: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qdialog.html#exec
我知道 utdemir 的回答解决了您的问题,但我只是想说 Qt 附带了几个方便的输入对话框。例如,看看 QInputDialog.getText 。
I know that utdemir's response solved your problem but I just wanted to say that Qt comes with several convenience input dialogs included. Have a look at QInputDialog.getText for instance.