从主 PyQt 窗口启动 PyQT 窗口,并获取用户输入?

发布于 2024-11-29 11:49:19 字数 1348 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去 2024-12-06 11:49:19

使用

inputter.exec_()

而不是

inputter.show()

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

此方法也是具有 C++ 签名 int exec() 的 Qt 槽。

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

如果对话框是应用程序模式,则用户无法与任何对话框交互
同一应用程序中的其他窗口,直到关闭对话框。如果
该对话框是窗口模式的,仅与父窗口交互
对话框打开时被阻止。默认情况下,该对话框是
应用程序模式。

另请参见 open()、show()、result() 和 setWindowModality()。

Use

inputter.exec_()

instead of

inputter.show()

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

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

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.

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

本王不退位尔等都是臣 2024-12-06 11:49:19

我知道 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文