PyQt5各种常用对话框总结

QDialog基本对话框

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

显示基本窗体

测试代码

# -*- coding:utf8 -*-
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication

class 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.xxx
Abort = 262144
AcceptRole = 0
ActionRole = 3
Apply = 33554432
ApplyRole = 8
ButtonMask = -769
Cancel = 4194304
Close = 2097152
Critical = 3
Default = 256
DestructiveRole = 2
Discard = 8388608
Escape = 512
FirstButton = 1024
FlagMask = 768
Help = 16777216
HelpRole = 4
Ignore = 1048576
Information = 1
InvalidRole = -1
LastButton = 134217728
No = 65536
NoAll = 131072
NoButton = 0
NoIcon = 0
NoRole = 6
NoToAll = 131072
Ok = 1024
Open = 8192
Question = 4
RejectRole = 1
Reset = 67108864
ResetRole = 7
RestoreDefaults = 134217728
Retry = 524288
Save = 2048
SaveAll = 4096
Warning = 2
Yes = 16384
YesAll = 32768
YesRole = 5
YesToAll = 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 QInputDialog
number, 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 QFontDialog
font, ok = QFontDialog.getFont()
if(ok):
    # 设置字体样式函数
    self.lintEdit.setFont(font)

QColorDialog颜色选取对话框

测试代码

from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QColorDialog
color = 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)

原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/194126.html

(0)
上一篇 2021年11月15日
下一篇 2021年11月15日

相关推荐

发表回复

登录后才能评论