gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:09 30 4
gpt4 key购买 nike

我有一个 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 关闭。那么,如何让它等待,以便我可以检索用户输入?

最佳答案

使用

inputter.exec_()

代替

inputter.show()

发件人: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().

关于python - 从主 PyQt 窗口启动 PyQT 窗口,并获取用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7046882/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com