QDialog控件的作用
QDialog控件用于弹出一个对话框窗口,用于进行人机交互。
QDialog类的子类主要有:QMessageBox.QFileDialog,QfontDialog,QinputDialo,QColorDialog等
QDialog控件常用方法
代码示例
# -*- coding: utf-8 -*- import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class DialogDemo(QMainWindow): def __init__(self, parent=None): super(DialogDemo, self).__init__(parent) self.setWindowTitle("Dialog 示例") self.resize(350,300) self.btn = QPushButton( self) self.btn.setText("弹出对话框") self.btn.move(50,50) # 点击按钮弹出一个新的对话框 self.btn.clicked.connect(self.showdialog) def showdialog(self ): dialog = QDialog() btn = QPushButton("ok", dialog) btn.move(50,50) dialog.setWindowTitle("Dialog") dialog.setWindowModality(Qt.ApplicationModal) dialog.exec_() if __name__ == '__main__': app = QApplication(sys.argv) demo = DialogDemo() demo.show() sys.exit(app.exec_())
运行结果:
点击按钮运行结果: