千家信息网

PyQt5各种常用对话框总结

发表于:2025-01-24 作者:千家信息网编辑
千家信息网最后更新 2025年01月24日,QDialog基本对话框子类主要有QMessageBox、QFileDialog、QFontDialog、QInputDialog等显示基本窗体测试代码# -*- coding:utf8 -*-imp
千家信息网最后更新 2025年01月24日PyQt5各种常用对话框总结

QDialog基本对话框

子类主要有QMessageBox、QFileDialog、QFontDialog、QInputDialog等

显示基本窗体

测试代码

# -*- coding:utf8 -*-import sysfrom PyQt5.QtCore import Qtfrom PyQt5.QtWidgets import QMainWindow, QApplicationclass MyDialog(QMainWindow):    def __init__(self, parent=None):        super(MyDialog, self).__init__(parent)        # 设置窗口标题        self.setWindowTitle("测试窗口")        # 设置窗口大小        self.resize(350, 300)        # 设置窗口为非模态        self.setWindowModality(Qt.NonModal)        # 设置窗口为窗口模态,程序在未处理完当前对话框时将阻止和对话框的父窗口进行交互        self.setWindowModality(Qt.WindowModal)        # 设置窗口为应用程序模态,阻止和人任何其他窗口进行交互        self.setWindowModality(Qt.ApplicationModal)if __name__ == '__main__':    app = QApplication(sys.argv)    ui_demo = MyDialog()    ui_demo.show()    sys.exit(app.exec_())

QMessageBox通用式弹出对话框

对话框函数

# 信息提示对话框QMessageBox.information(ParentQWidght, Title, Text, Buttons, DefaultButton)# 问题响应对话框QMessageBox.question(ParentQWidght, Title, Text, Buttons, DefaultButton)# 警告对话框QMessageBox.warning(ParentQWidght, Title, Text, Buttons, DefaultButton)# 关于对话框QMessageBox.about(ParentQWidght, Title, Text, Buttons, DefaultButton)# 严重警告对话框QMessageBox.critical(ParentQWidght, Title, Text, Buttons, DefaultButton)

PyQt5源码中定义的按钮

# 调用方法:QMessage.xxxAbort = 262144AcceptRole = 0ActionRole = 3Apply = 33554432ApplyRole = 8ButtonMask = -769Cancel = 4194304Close = 2097152Critical = 3Default = 256DestructiveRole = 2Discard = 8388608Escape = 512FirstButton = 1024FlagMask = 768Help = 16777216HelpRole = 4Ignore = 1048576Information = 1InvalidRole = -1LastButton = 134217728No = 65536NoAll = 131072NoButton = 0NoIcon = 0NoRole = 6NoToAll = 131072Ok = 1024Open = 8192Question = 4RejectRole = 1Reset = 67108864ResetRole = 7RestoreDefaults = 134217728Retry = 524288Save = 2048SaveAll = 4096Warning = 2Yes = 16384YesAll = 32768YesRole = 5YesToAll = 32768

测试代码

from PyQt5.QtWidgets import QMessageBox # 仅仅显示if(msg=='connect success'):    conn.close()    QMessageBox.information(self, "温馨提示", "数据库连接成功!", QMessageBox.Yes, QMessageBox.Yes)else:    QMessageBox.warning(self, "警告信息", "由于目标计算机积极拒绝, 无法连接, 来自数据库的消息:{0}".format(msg), QMessageBox.Yes, QMessageBox.Yes)# 处理用户应答后的动作reply = QMessageBox.warning(self, "温馨提示", "即将退出, 确定?", QMessageBox.Yes|QMessageBox.No, QMessageBox.No)if(reply == QMessageBox.Yes):    QCloseEvent.accept()if(reply==QMessageBox.No):    QCloseEvent.ignore()

QInputDialog标准输入对话框

常用取值函数

# 获取实数QInputDialog.getDouble(ParentQWidght,Title,Text)# 获取整数QInputDialog.getInt(ParentQWidght,Title,Text)# 在控件中的获得列表中获取输入QInputDialog.getItem(ParentQWidght,Title,Text,DefaultItem, isOperation)# 获取文本QInputDialog.getText(ParentQWidght,Title,Text)# 获取多行文本QInputDialog.getMultiLineText(ParentQWidght,Title,Text,DefaultText)

测试代码

from PyQt5.QtWidgets import QInputDialognumber, ok = QInputDialog.getInt(self, "输入数字对话框", "请输入数字")if(ok):    print(number)number, ok = QInputDialog.getDouble(self, "输入数字对话框", "请输入数字")if(ok):    print(number)text, ok = QInputDialog.getText(self, "输入数字对话框", "请输入数字")if(ok):    print(text)mul_text, ok = QInputDialog.getMultiLineText(self, "xixi", "haha", "hehe")if(ok):    print(mul_text)item, ok = QInputDialog.getItem(self, "", "", ['One', 'Two', 'Three'], 0, False)if(ok):    print(item)

QFontDialog字体选择对话框

测试代码

from PyQt5.QtWidgets import QFontDialogfont, ok = QFontDialog.getFont()if(ok):    # 设置字体样式函数    self.lintEdit.setFont(font)

QColorDialog颜色选取对话框

测试代码

from PyQt5.QtGui import QPalettefrom PyQt5.QtWidgets import QColorDialogcolor = QColorDialog.getColor()if(color.isValid()):    # 设置颜色名称    self.label.setText(color.name())    # 设置颜色    self.label.setPalette(QPalette(color))    # 设置自动填充背景颜色    self.label.setAutoFillBackground(True)

QFileDialog文件选择对话框

常用函数

# 获取文件的打开路径QFileDialog.getOpenFileName()# 获取一组文件的打开路径QFileDialog.getOpenFileNames()# 获取文件的保存路径QFileDialog.getSaveFileName()# 设置可以选择的文件类型QFileDialog.setFileMode()"""枚举常量包括:    QFileDialog.AnyFile    QFileDialog.ExistingFile    QFileDialog.Directory    QFileDialog.ExistingFiles"""# 设置文件过滤器,显示的文件类型QFileDialog.setFilter()# 获取已经存在的目录QFileDialog.getExistingDirectory()

测试代码

file_name, ok = QFileDialog.getOpenFileName(self, "打开文件", '/', "Images files (*.jpg *.gif)")if(ok):    print(file_name)files_name, ok = QFileDialog.getOpenFileNames(self, "打开一组文件", '/', "All Files (*);;Text Files (*.txt)")if(ok):    for i in files_name:        print(i)file_name, ok = QFileDialog.getSaveFileName(self, "保存文件", '/', "Images files (*.jpg *.gif)")if(ok):    print(files_name)QFileDialog.setFileMode(QFileDialog.AnyFile)QFileDialog.setFilter("Images files (*.jpg *.gif)")path = QFileDialog.getExistingDirectory(self, "选择目录", '/')print(path)
0