QDialog通常提供一个窗口用来收集用户的响应。它可以设为模态(阻塞其父窗口)和非模态(可以绕过该对话窗口)。
PyQt API也提供了一些配置好的对话框控件,如之前提到的InputDialog、FileDialog、FontDialog。
栗子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import sys from PyQt4.QtCore import * from PyQt4.QtGui import *
class window(QWidget): def __init__(self, parent=None): super(window, self).__init__(parent) self.init()
def init(self): layout = QHBoxLayout() button = QPushButton() button.setText(u"点我弹出窗口B") button.clicked.connect(self.showB) layout.addWidget(button) self.setLayout(layout) self.setWindowTitle(u"窗口A")
def showB(self): self.dialog = QDialog() buttonB = QPushButton(u"关了B",self.dialog) buttonB.clicked.connect(self.closeB) self.dialog.setWindowTitle(u"窗口B") self.dialog.setWindowModality(Qt.ApplicationModal) self.dialog.exec_()
def closeB(self): self.dialog.close()
def main(): app = QApplication(sys.argv) ex = window() ex.show() sys.exit(app.exec_())
if __name__ == '__main__': main()
|