• GUI编程--PyQt5--QDiaglog


    QDialog

    1. 对话框基类,继承QWidget;
    2. 用于短期任务,分为模态、非模态
      模态,阻塞在当前窗口;分为应用程序级别&窗口级别(仅阻塞关联的窗口);如文件选择。
      非模态,不阻塞;如查找

    应用程序级别的模态

    # 实例化对话框
    dialog = QDialog()
    # 应用程序级别(模态阻塞),只有当前窗口处理完,才可以继续往下走
    dialog.exec()
    
    • 1
    • 2
    • 3
    • 4

    窗口的模态,只阻塞相关的窗口

    # 实例化对话框
    dialog = QDialog(self)
    dialog.setWindowTitle("窗口模态")
    # 窗口级别(模态阻塞),只阻塞相关联的窗口
    dialog.open()
    
    # 非模态(非阻塞)
    # 转为模态 
    # dialog.setModal(True)
    dialog.setWindowModality(Qt.WindowModality.WindowModal)
    dialog.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    在这里插入图片描述

    # 右下角尺寸调整
    dialog.setSizeGripEnabled(True)
    
    # 接收
    dialog.accept()  # 返回1  如打开
    dialog.reject()  # 0   如取消
    dialog.done(6)   # 完成  返回6
    dialog.setResult(2)  # 设置结果
    dialog.result()   # 获取结果
    
    # 信号
    dialog.accepted.   # dialog.accept() 后发送信号
    dialog.rejected.   # dialog.reject() 后发送信号
    dialog.finished.   # 接收、拒绝后都会发送完成信号
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    QFontDialog

    选择字体对话框

    # 实例化
    fd = QFontDialog(win)
    
    fd2 = QFontDialog(QFont, win) # 传入一个默认字体
    
    # 当前字体(选择时的字体)
    fd.setCurrentFont(QFont)
    fd.currentFont()
    # 选择后的字体
    fd.selectedFont()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    选择字体:

    def set_ui(self):
    	# 实例化对话框
    	dialog = QFontDialog(self)
    	dialog.setWindowTitle("选择字体")
    	#
    	btn = QPushButton("选择字体", self)
    	btn.adjustSize()
    	btn.move(100, 100)
    	
    	def func():
    	    # 窗口选择后的字体QFont
    	    print("选择的字体:", dialog.selectedFont().family())
    	
    	# 窗口模态,传入槽函数
    	btn.clicked.connect(lambda : dialog.open(func))
    
    # fd.exec()    确定返回1  取消返回0
    
    # fd.show()   # 非模态  用于实时的展示
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    选项设置

    fd.setOption(QFontDialog.FontDialogOption.NoButtons)
    fd.setOptions(xxx | xxx)
    fd.testOption(xxx)  # 测试是否生效
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    静态方法,getFont(self)
    在这里插入图片描述

    def func():
        # 窗口选择后的字体QFont
        # print("选择的字体:", dialog.selectedFont().family())
        font, label = QFontDialog.getFont(self)  # self 为父控件
        # font 返回的QFont对象,不管点击的 确定、还是取消,均返回字体对象
        # label  bool值   点击”确定“ 则返回True,否则返回False
        if label:
            # 点击“确定”
            print("选择了字体:", font)
    
    # 窗口模态,传入槽函数
    # btn.clicked.connect(lambda : dialog.open(func))
    btn.clicked.connect(func)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

     
     

    QColorDialog

    选择颜色对话框
    如下,设置主窗口的背景色:
    在这里插入图片描述

    def set_ui(self):
        # 实例化对话框
        dialog = QColorDialog(self)
        dialog.setWindowTitle("选择颜色")
        # dialog.setOption(QColorDialog.ColorDialogOption.ShowAlphaChannel)
        #
        btn = QPushButton("选择颜色", self)
        btn.adjustSize()
        btn.move(100, 100)
    
        def func(color):  # 传入选择的颜色
            # 窗口选择后的颜色QColor
            print("选择的颜色:", color)
            palette = QPalette()
            palette.setColor(QPalette.Background, color)
            # 父窗口设置背景色
            self.setPalette(palette)
    
        # 窗口模态,传入槽函数
        # btn.clicked.connect(lambda : dialog.open(func))
        btn.clicked.connect(lambda : dialog.show())
        # 颜色被选择后
        dialog.colorSelected.connect(func)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    其他操作:

    colorDialog.setOption()
    colorDialog.setOptions()
    colorDialog.setCurrentColor()
    colorDialog.currentColor()
    # 信号与槽
    colorDialog.currentColorChanged.connect(func)
    colorDialog.colorSelected.connect(func)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    静态方法:

    # 自定义颜色个数
    cd.customCount()
    
    # 设置用户自定义颜色,必须在QColorDialog实例化前
    QColorDialog.setCustomColor(idx, QColor)
    # 设置标准颜色
    QColorDialog.setStandardColor(idx, QColor)
    
    # 静态方法选择颜色
    color = QColorDialog.getColor(QColor, self, "颜色标题")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

     
     

    QFileDialog

    选择文件对话框
    控件实例化

    def set_ui(self):
        # 实例化对话框
        dialog = QFileDialog(self)
        # 设置窗口标题
        dialog.setWindowTitle("选择文件")
        # 设置默认打开的目录
        dialog.setDirectory("D:/tempSoft/laufing")
        # 设置目录过滤器
        dialog.setFilter(QDir.Filter.AllDirs)
        # 设置文件名 过滤器
        dialog.setNameFilter("All(*.*);;Image(*.png *.jpg);;Python文件(*.py)")
        # dialog.setNameFilters(["All(*.*)", "Image(*.png *.jpg)",])
    	
    	# 设置accept模式  是“保存”按钮 还是 “打开”按钮
    	dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave)
    	# 保存文件时的后缀扩展名
    	dialog.setDefaultSuffix(".txt")
    	
    	# 设置选择的文件模式(文件、目录)
    	dialog.setFileMode(QFileDialog.FileMode.AnyFile)
    	# QFileDialog.FileMode.AnyFile
        # QFileDialog.FileMode.ExistingFile
        # QFileDialog.FileMode.Directory
        
        # 显示的详细情况
        dialog.setViewMode()
    
    	# 改变窗口的文本字样
    	dialog.setLabelText(QFileDialog.DialogLabel.FileName, "laufing")
    	dialog.setLabelText(QFileDialog.DialogLabel.Accept, "receive")
    	dialog.setLabelText(QFileDialog.DialogLabel.Reject, "reject")
    	
        #dialog.setOption(QFileDialog.FileMode.AnyFile)
        #
        btn = QPushButton("选择文件", self)
        btn.adjustSize()
        btn.move(100, 100)
    
        def func(file):  # 传入选择的文件
            # 窗口选择后的文件绝对路径
            print("选择的文件:", file)  # file 文件的绝对路径
    
    
        # 窗口模态,传入槽函数
        btn.clicked.connect(lambda : dialog.show())
        # 文件选择后
        dialog.fileSelected.connect(func)
    
    • 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
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

     
     
    静态方法
    在这里插入图片描述

    # 静态方法使用格式
    file_abs_path, filter_str = QFileDialog.getOpenFileName(父控件, "windowTitle", "打开目录", "过滤器All(*.*);;Images(*.png *.jpg);;Python文件(*.py)", "默认的过滤器")
    
    # 使用静态方法 获取选择的文件
    def set_ui(self):
        btn = QPushButton("选择文件", self)
        btn.adjustSize()
        btn.move(100, 100)
    
        def func():  #
            # 窗口选择后的文件 (实例化也可以按照这种传参)
            abs_file, filter_str = QFileDialog.getOpenFileName(self, "选择文件", "D:/",
                                                               "All(*.*);;Image(*.png *.jpg);;Python文件(*.py)",
                                                               "Image(*.png *.jpg)")
            print("选择的文件:", abs_file)
            print("默认的过滤器:", filter_str)
    
    
        # 信号与槽
        btn.pressed.connect(func)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述
    其他的静态方法使用大致相同。
    获取保存的文件名后,就可以写入内容。

    获取目录:

     # 选择目录
     def func():  #
        # 窗口选择后的目录
        abs_path = QFileDialog.getExistingDirectory(self, "选择目录", "D:/")
        print("选择的目录:", abs_path)
    
    
    # 选择目录url
    abs_path_url = QFileDialog.getExistingDirectoryUrl(self, "选择目录", QUrl("D:/"))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    信号
    在这里插入图片描述

     
     

    QInputDialog

    输入内容对话框

    静态方法获取内容

    # 整数值
    result = QInputDialog.getInt(self, "大标题", "小标题", default, min, max, step)
    # 小数值
    result = QInputDialog.getDouble(self, "大标题", "小标题", default, decimals=3)
    # 文本
    result = QInputDialog.getText(self, "大标题", "小标题", echo=QLineEdit.EchoMode.Password)
    # 多行文本
    result = QInputDialog.getMultiLineText(self, "大标题", "小标题", default)
    # 列表项
    result = QInputDialog.getItem(self, "大标题", "小标题", ["a", "b", "c"], default_idx)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    # 小数
    def func():  #
        # 窗口选择后的文件
        value = QInputDialog.getDouble(self, "大标题", "小标题", 10.2, min=0.0, max=100.0, decimals=1)
        print("选择的文件:", value)
    
    # 列表项
    def func():  #
               # 窗口选择后的文件
               value = QInputDialog.getItem(self, "大标题", "小标题", ["a", "b", "c"], 2)
               print("选择的文件:", value)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

     
    实例化

    # 实例化
    input_dialog = QInputDialog(self, Qt.WindowType.FramelessWindowHint)
    # 设置选项
    input_dialog.setOption()
    # 设置文本
    input_dialog.setLabelText("输入信息")
    input_dialog.setOkButtonText("确定")
    input_dialog.setCancleButtonText("取消")
    #  输入模式
    input_dialog.setInputMode(QInputDialog.InputMode.DoubleInput)
    input_dialog.setDoubleRange(0.0, 10.0)
    input_dialog.setDoubleStep(2.5) # 步长
    input_dialog.setDoubleDecimals(2)  # 小数位数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述
    在这里插入图片描述

    信号
    在这里插入图片描述

  • 相关阅读:
    【AGC】【Connect API】如何获取全部应用的appId
    tensorflow中的slim函数集合
    【Vue】v-model修饰符
    三季度都快过完了,看看项目经理年初立的flag怎样了?
    Python 迭代器与生成器
    高防IP是什么意思?
    python经典百题之用*号输出字母C的图案
    【云原生-Docker篇】之 Docker Registry的搭建与使用
    C语言的goto err
    STM32产品介绍
  • 原文地址:https://blog.csdn.net/weixin_45228198/article/details/128045891