从 PyQt 主窗口启动 PyQT 窗口,并获取用户输入? [英] launch a PyQT window from a main PyQt window, and get the user input?

查看:50
本文介绍了从 PyQt 主窗口启动 PyQT 窗口,并获取用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PyQt 主窗口,当他们点击某个按钮时,我需要从中获取一串用户输入.

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)

从主窗口,我说:

inputter = InputDialog(mainWindowUI, title="comments", label="comments", text="")
inputter.show()
comment = inputter.text.text()
print comment

这会打印一个空字符串,即使用户输入评论并点击确定".显然是因为主窗口脚本不会等待 InputDialog 关闭.那么,如何让它等待,以便我可以检索用户输入?

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?

推荐答案

使用

inputter.exec_()

代替

inputter.show()

来自:http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qdialog.html#exec

这个方法也是一个带有C++签名int exec()的Qt槽.

This method is also a Qt slot with the C++ signature int exec().

将对话框显示为模态对话框,在用户关闭它之前一直处于阻塞状态.该函数返回一个 DialogCode 结果.

Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result.

如果对话框是应用程序模式,用户不能与任何同一个应用程序中的其他窗口,直到他们关闭对话框.如果对话框是窗口模态的,只有与父窗口的交互是对话框打开时被阻止.默认情况下,对话框是应用模式.

If the dialog is application modal, users cannot interact with any other window in the same application until they close the dialog. If the dialog is window modal, only interaction with the parent window is blocked while the dialog is open. By default, the dialog is application modal.

另见 open()、show()、result() 和 setWindowModality().

See also open(), show(), result(), and setWindowModality().

这篇关于从 PyQt 主窗口启动 PyQT 窗口,并获取用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆