python启动qt5窗口_python – 从主PyQt窗口启动一个PyQT窗口,并获取用户输入?

在Python的PyQt5应用中,当用户点击主窗口的按钮时,需要弹出一个对话框以获取用户输入。问题在于,使用`show()`方法会导致主窗口无法等待用户输入完成。解决办法是使用`exec_()`替代`show()`,`exec_()`会阻塞程序直到用户关闭对话框,从而能够正确获取用户输入。
摘要由CSDN通过智能技术生成

我有一个主要的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()

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().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值