您需要使用QDialog并使用exec对其进行显示,这将阻塞应用程序的其余部分,直到将其关闭。返回值
exec还告诉您是否在不提交更改(即取消)的情况下关闭了表单。
这是一个简单的演示脚本,显示了如何使用
QDialog:
from PyQt4 import QtCore, QtGuiclass Dialog(QtGui.QDialog): def __init__(self, parent=None): QtGui.QDialog.__init__(self, parent) self.checkbox1 = QtGui.QCheckBox('Option one', self) self.checkbox2 = QtGui.QCheckBox('Option two', self) self.buttonOk = QtGui.QPushButton('Ok', self) self.buttonOk.clicked.connect(self.accept) self.buttonCancel = QtGui.QPushButton('Cancel', self) self.buttonCancel.clicked.connect(self.reject) layout = QtGui.QGridLayout(self) layout.addWidget(self.checkbox1, 0, 0, 1, 2) layout.addWidget(self.checkbox2, 1, 0, 1, 2) layout.addWidget(self.buttonOk, 2, 0) layout.addWidget(self.buttonCancel, 2, 1)class Window(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) widget = QtGui.QWidget(self) layout = QtGui.QVBoxLayout(widget) self.button = QtGui.QPushButton('Show Dialog', self) self.button.clicked.connect(self.handleButton) layout.addWidget(self.button) self.setCentralWidget(widget) def handleButton(self): dialog = Dialog(self) if dialog.exec_() == QtGui.QDialog.Accepted: print('Option one: %s' % dialog.checkbox1.isChecked()) print('Option two: %s' % dialog.checkbox2.isChecked()) else: print('Cancelled') dialog.deleteLater()if __name__ == '__main__': import sys app = QtGui.QApplication(sys.argv) window = Window() window.setGeometry(500, 300, 200, 100) window.show() sys.exit(app.exec_())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)