Pyqt主要组件QFileDialog,QMessageBox,QInputDialog,QPushButton详解,附详细代码(持续更新中.....)


Author:qyan.li

Date:2022.4.24

Topic:学习整理QT for python中各组件的基本用法和示例Demo

Reference:https://blog.51cto.com/quantfabric/2422601

一、写在前面

       ~~~~~~        最近计划开始一篇新的博文,内容主要是Qt for python基本组件的整理和示例Demo,一方面帮助老师搜集一下相关知识,另一方面也希望系统的学习下QT组件的相关知识,作为之前

树形组件的补充和拓展。关于树形组件的基本内容,可以参考我之前的博文,链接:https://blog.csdn.net/DALEONE/article/details/123676385?spm=1001.2014.3001.5501

       ~~~~~~        同时,我大概会每天更新一点,会持续一段时间,在这个过程中,更新可能会不及时。

二、QT for python基础组件

       ~~~~~~       今天会更新几个弹窗类的组件,这类组件在QT制作中是至关重要的,比如文件打开,选择,保存的弹窗以及窗口关闭时的警告文件。

1. QFileDialog组件

       ~~~~~~        印象中,应该是在树形组件博文中简单介绍过此组件,官网对其的界定为The QFileDialog class provides a dialog that allows users to select files or directories,翻译过来就是提供一个对话框供用户选择文件或者目录。

       ~~~~~~        在官网中,QFileDialog提供两种构造的方式:1. 借助于静态函数-static function 2. 自己创建

  1. 借助于静态函数-getOpenFileName

    先上代码:

    fname, _ = QFileDialog.getOpenFileName(self, "Open file", 'C:/Users\腻味\Desktop\ClashForWindows', "Images(*.jpg *.gif);;Text files (*.txt)") # 选择文件并将选择的文件路径进行返回
    print(fname)
    

    Tips:

    1. 常用的几个参数:

      caption:{str}对应于"open file"用于定义打开窗口的名称

      dir:{str}用于定义打开窗口默认的文件路径

      selectedFilter:{str}用于定义待操作文件的可选类型

    2. 函数返回值:

      fileName,selectedFilter原代码中的_即代表selectedFilter,用下划线表示我们不关心,但是原函数确实返回,故借其占位

    3. 其他:

      官方文档永远是最好的参考文档,getOpenFileName更多可选参数见官方文档,传送门:QFileDialog — Qt for Python

  2. 借助于函数自己创建

           ~~~~~~        相比于使用官方提供的静态函数,借助于函数自我创建的方法显然更加复杂,但借助于此种方法的优点在于限制小,可操作性大

    老样子,先上代码:

    fileNames = [] 
    dialog = QFileDialog(self) # QFileDialog类实例化
    dialog.setFileMode(QFileDialog.AnyFile)
    
    ## 指定打开的文件类型:源代码中添加tr,但查阅相关资料后发现tr可以不适用,且一般情况下不推荐使用
    # 参考文献:https://blog.csdn.net/weixin_41567783/article/details/118416484
    dialog.setNameFilter("Images (*.png *.xpm *.jpg)")
    
    ## 理论上,list仅显示文件名和文件夹列表,而Details同时显示详细信息(文件大小,修改时间等)
    # dialog.setViewMode(QFileDialog.Detail)
    # dialog.setViewMode(QFileDialog.List)
    
    dialog.setDirectory('C:/Users\腻味\Pictures\Saved Pictures') ## 此函数可以设计打开时的文件夹
    

    Tips:

    1. setFileMode函数用于设置FileMode,此处有三种模式可供选择:AnyFile,ExistingFile,Directory

      • AnyFile可选任何类型,甚至不存在的文件,因此常用于save as类的对话框
      • ExistingFile可选确实存在的文件
      • Directory可选目录,不可选具体文件
    2. setNameFilter用于指定文件的待选格式,如果阅读源码会发现源码中字符串前加tr,但查阅资料未找到tr的相关定义,最终在一篇博文中提及:tr似乎是为方便语言切换设计的模块,此处可以不使用

      Reference:https://blog.csdn.net/weixin_41567783/article/details/118416484

    3. setViewMode设置文件显示信息,从官方文档看,其包含有两种模式QFileDialog.Detail和QFileDialog.List,一种仅显示文件列表,另外一种还可以显示文件详细信息(如文件大小,修改时间等),但从代码实际运行的效果观察,并未起到作用,无论设置任何Mode,最终显示均为详细信息

    4. SetDirectory用于设定对话框打开默认的文件路径,内部参数传入DirPath即可

    5. 更多信息参照官方文档

最后,附上总体代码:

## QFileDialog模块学习
import sys
from PyQt5.QtWidgets import QApplication, QFileDialog, QWidget, QPushButton

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        button = QPushButton("选择文件", self)

        self.resize(800,600)
        button.clicked.connect(self.onOKClicked) 

    def onOKClicked(self):
        
        '''1. Using a static function-getOpenFileName'''
        # fname, _ = QFileDialog.getOpenFileName(self, "Open file", 'C:/Users\腻味\Desktop\ClashForWindows', "Images(*.jpg *.gif);;Text files (*.txt)") # 选择文件并将选择的文件路径进行返回
        # print(fname)

        '''2. creat our own QFileDialog'''
        fileNames = [] # 首先赋值为empty directory
        dialog = QFileDialog(self)
        dialog.setFileMode(QFileDialog.AnyFile)
        dialog.setNameFilter("Images (*.png *.xpm *.jpg)")
        
        # # dialog.setViewMode(QFileDialog.Detail)
        # # dialog.setViewMode(QFileDialog.List)
        dialog.setDirectory('C:/Users\腻味\Pictures\Saved Pictures') 

        if dialog.exec_():
            fileNames = dialog.selectedFiles()
        print(fileNames)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

2022.4.25我又来更新啦,今天主要更新QMessageBox组件


2. QMessageBox组件

       ~~~~~~       QMessageBox主要用于弹出对话框显示信息提示和指导用户,官方文档给出的定义为The QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer,翻译过来简单讲就是给予用户信息,询问用户问题,接收用户输入。

       ~~~~~~       QMessageBoxQT创建中也是非常常见的,在警告框,确认框等方面应用颇多,下面也还是主要介绍QMessageBox的两种构造方法:

  1. 借助于静态函数information,critical,question,warning

    老样子,还是先上代码:

    ret = QMessageBox.question(self, "question Message",
                                   "The document has been modified.\n Do you want to save your changes?",
                                   QMessageBox.Save | QMessageBox.Discard
                                   | QMessageBox.Cancel,
                                   QMessageBox.Save)
    

    上述代码展示如何借助于静态函数实现询问对话框的制作

    Tips:

    1. 关于question函数的几点说明:

      关于question函数,官网给出两种构造方法,简单总结下:

      • question(parent,title,text,button1,button2),返回值为int
      • question(parent,title,text,buttons,defaultButton),返回值为StandardButton

      在本例中,使用的为重载过后的question方法,QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel表示Buttons,而QMessageBox.Save表示defaultButton

      这一点可以在代码中借助于print('type(ret)')说明,发现输出值为<class 'PyQt5.QtWidgets.QMessageBox.StandardButton'>(虽然此处代码直接输出为数字)

    2. 关于重载函数中buttonsdefaultButton的说明:

      buttons使用|连接,表示会直接在对话框中显示的按钮,而defaultButton则表示默认情况下按钮情况

    3. question,warning,critical,information按钮创建和使用方法类似,区别仅在于对话框左侧的图形显示不同,具体内容详见官方文档

  2. 借助于函数定义方法自己创建

           ~~~~~~       与QFileDialog相似,借助于函数定义的方法创建比静态方法更自由,限制小,但是创建的难度和复杂度比静态方法要高。

    老样子,先上代码:

    msgBox = QMessageBox() # QMessageBox class 实例化 
    msgBox.setText("The document has been modified.") 
    msgBox.setInformativeText("Do you want to save your changes?")
    
    msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
    msgBox.setDefaultButton(QMessageBox.Save)
    msgBox.setDetailedText("这是一个测试用例\n用于测试setDetailedText的具体用法")
    
    ret = msgBox.exec()
    print(ret == QMessageBox.Save)
    

    Tips:

    1. setTextsetInformativeText可以用于设置在对话框中显示的信息,内部传入str类型字符串即可

    2. setStandardButtonssetDefaultButton分别用于设置标准按钮和默认按钮,与静态方法创建时相同

    3. setDetailedText用于添加详细信息,调用函数后对话框上会自动添加show Details按钮,点击后即可显示详细信息,内部传入str字符串即可

    4. ret = msgBox.exec()用于调用对话框,该函数必须被调用,否则对话框不显示,看不见任何效果

      同时ret用于接收函数返回值(用户点击的按钮),因此,假设用户此时点击saveprint的输出值为true

最后,附上总代码,方便大家参考借鉴:

import sys
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget, QPushButton


class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        button = QPushButton("OK", self) ## 界面中设置一个Button

        self.resize(800, 600)
        button.clicked.connect(self.onOKClicked)

    def onOKClicked(self):
        msgBox = QMessageBox()
        ## 显示信息
        msgBox.setText("The document has been modified.")
        msgBox.setInformativeText("Do you want to save your changes?")
        ## 小界面中设置Button
        msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
        msgBox.setDefaultButton(QMessageBox.Save)
        
        # 设置详细信息的显示,会自动添加Button按钮
        msgBox.setDetailedText("这是一个测试用例\n用于测试setDetailedText的具体用法")

        ## ret本质上会返回用户选择的按钮
        ret = msgBox.exec()
        print(ret == QMessageBox.Save)
        

        '''借助于静态函数进行实例化'''
        # ## warning的弹出框
        # ret = QMessageBox.warning(self, "Warning Message",
        #                        "The document has been modified.\n Do you want to save your changes?",
        #                        QMessageBox.Save | QMessageBox.Discard
        #                        | QMessageBox.Cancel,
        #                        QMessageBox.Save)
        
        # ## information的弹出框
        # ret = QMessageBox.information(self, "Information Message",
        #                        "The document has been modified.\n Do you want to save your changes?",
        #                        QMessageBox.Save | QMessageBox.Discard
        #                        | QMessageBox.Cancel,
        #                        QMessageBox.Save)
        
        # # ## question的弹出框 
        # ret = QMessageBox.question(self, "Question Message",
        #                        "The document has been modified.\n Do you want to save your changes?",
        #                        QMessageBox.Save | QMessageBox.Discard
        #                        | QMessageBox.Cancel,
        #                        QMessageBox.Save)
        
        # ## critical的弹出框
        # ret = QMessageBox.critical(self, "critical Message",
        #                        "The document has been modified.\n Do you want to save your changes?",
        #                        QMessageBox.Save | QMessageBox.Discard
        #                        | QMessageBox.Cancel,
        #                        QMessageBox.Save)
        # print(type(ret))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

2022.4.27 时隔两天,我终于又来更新啦!今天的内容为QInputDialog板块


3. QInputDialog组件

       ~~~~~~        QInputDialog组件是一个可接收用户输入的对话框,官网给出的解释为:The QInputDialog class provides a simple convenience dialog to get a single value from the user,翻译过来即为提供一个方便的对话框用于接收用户输入。

       ~~~~~~       QInputDialog与上述组件相同,同样提供静态函数便捷的创建对话框实例,提供的静态函数主要有 getDouble() , getInt() , getItem() , getText() , getMultiLineText(),下面我们主要介绍其中的两种用于说明此静态函数的使用方法:

  1. getInt()静态函数:

    老样子,先上代码:

    num, ok = QInputDialog.getInt(self, "Input an int number", "num:")
    if ok:
    	print("input num: ", num)
    

    getMultiLineText()静态函数:

    text, ok = QInputDialog.getMultiLineText(self, "Input MultiLineText", "Text:")
    if ok:
    	print("input text: ", text)
    

关于上述函数的一些小Tips:

  • 大家会发现getInt(),getMultiLineText()调用方式基本相同,实际上,其余静态函数的调用方式都类似(下面提及的getItem()函数传入的参数与其他有些不同)
  • 静态函数的输入参数主要包含两个:Input an int number为弹窗的标题,num:为在对话框中显示的具体内容
  • 静态函数的返回值有两个:valueok标签value表示用户输入的内容(可以为数字,字符串等等),ok返回bool值,表示用户是否进行选择
  • 上述展示的均为基本用法,更多详细的用法和函数见官方文档,参考链接:https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QInputDialog.html
  1. getItem()静态函数:

    老样子,还是先展示代码:

    items = ["C++", "Python", "Java", "Go"]
    item, ok = QInputDialog.getItem(self, "Select an Item", "Programing Language", items, 0, False)
    if ok and item:
    	print("selected item: ", item)
    

    Tips:

    1. getItem()与其余静态函数功能相同,同样是提供对话框供用户选择,但区别在于getItem给与用户一个区间供其选择,而并非用户随意输入
    2. getItem()接收输入的参数多些,前面的两个text相同,分别表示对话框标题和具体内容,后续需要接收三个参数,items---list,current---int,editable---bool三个参数分别代表,供用户选择的待选列表,列表默认显示字条index,是否可编辑
    3. 返回值与上述getInt()相同,更多详细信息参照官方文档

此处展示QInputDialog部分的完整代码,方便大家参考使用:

import sys
from PyQt5.QtWidgets import QApplication, QInputDialog, QWidget, QPushButton


class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        button = QPushButton("OK", self) 

        self.resize(800, 600)
        button.clicked.connect(self.onOKClicked)

    def onOKClicked(self):

        # QInputDialog提供多个用户选择getItem,getText,getInt,getDouble
        items = ["C++", "Python", "Java", "Go"]
        item, ok = QInputDialog.getItem(self, "Select an Item", "Programing Language", items, 1, True)
        if ok and item:
            print("selected item: ", item)

        text, ok = QInputDialog.getText(self, "Input an text", "text:")
        if ok:
            print("input text: ", text)

        
        num, ok = QInputDialog.getInt(self, "Input an int number", "num:")
        if ok:
            print("input num: ", num)



        text, ok = QInputDialog.getMultiLineText(self, "Input MultiLineText", "Text:")
        if ok:
            print("input text: ", text)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

       ~~~~~~        2022.4.28 今天开始,五一小长假正式开始,也有更充裕的时间可以好好的更新一下,今天更新重要模块QpushButton
       ~~~~~~        其中,给自己埋了一下小坑,过后会同步更新一下:有关**“python中lambda表达式,列表生成式和map函数”**的一个新文章


4. QPushButton组件

       ~~~~~~       QPushButton组件,顾名思义是定义QT界面中按钮的类,官网对其的界定为Perhaps the most commonly used widget in user's interface,足见其在QT界面设计中的重要性,但是从下面的介绍中,大家可以发现,QPushButton的组件使用相对比较简单。

       ~~~~~~       首先还是介绍一下QPushButton一些基本函数的使用方法,老样子,先上代码:

button1 = QPushButton("OK", self) ## QPushButton实例化
button1.clicked.connect(lambda: self.onClicked(button1))
def onClicked(self, button):
 	print("Button {0} is clicked.".format(button.text())) 

Tips:

  • QPushButton的构造方法中传入字符串str作为Buttonlabel

  • button.text()函数用于返回特定buttonlabel

  • 此处的connect函数调用相比于前序class传入的参数发生变化,此前均传入函数名称,但此处借助于lambda表达式传入函数信息

    此处使用lambda表达式的原因在于调用的目标函数需要参数传入,因此不能仅仅传入函数名称,必须连同参数一起传入,此种情况下需要借助于lambda表达式,

    关于lambda表达式的其他问题,可以参照另外一篇博客:https://mp.csdn.net/mp_blog/creation/success/124482843

       ~~~~~~       然后,讲述完QPushButton的基本使用信息,了解一些关于button的其他应用和其他函数:

  1. setText函数后续修改Button的标签信息:

    按钮的label设定主要有两种方法:

    • constructor方法传入:上述已经介绍

    • setText()方法后续修改:

      button1.setText("help")
      
  2. 快捷键设置:

           ~~~~~~       在解释QPushButton时,官网提及按钮快捷键设置的一种方法,在字符串前添加"&",注意:此时的快捷键是使用alt键&后的第一个字母组和确定

    button1.setText("&help")
    
    • 根据上述解释,此时button1的快捷键为alt+h

    • 顺便提醒一下:use && to display an actual ampersand使用双& 表示真正的&

  3. 默认按键设置setAutoDefault

           ~~~~~~       官网有关于默认键设置给出的说明为A Default button is a push button that is activated when the user presses the Enter or Return key in the dialog,翻译过来简单意思为将现有按钮设置为默认,在用户按下Enter或者Return时会被默认激活

    button1.setAutoDefault(True)
    

    经测试,按下回车空格均可以激活默认按钮

           ~~~~~~       OK,有关于QPushButton的基本用法说明完毕,照例放上完整代码方便大家参考:

    import sys
    from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
    
    
    class MainWidget(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            button1 = QPushButton("OK", self) ## 创建名为ok的Button
           
           ## label的内容可以在构造函数创建,也可借助于setText函数实现后期修改
            button1.setText("help")
          
          ## 设置默认按键驱动Button(Enter or Return key in the dialog)
            button1.setAutoDefault(False)
           
           ## 快捷键设置(简单的快捷键的设置方法:字母前加&,快捷键即为alt+字母)
            # button1.setText("&help")
    
            button1.clicked.connect(lambda: self.onClicked(button1)) 
            
    
        def onClicked(self, button):
            print("Button {0} is clicked.".format(button.text())) 
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainWidget()
        window.resize(400, 200)
        window.show()
    
        sys.exit(app.exec_())
    

2022.4.28 坑终于填完啦!!!((●’◡’●))


这边坑就不填啦!完整版请参照新的博文:https://blog.csdn.net/DALEONE/article/details/125665819?spm=1001.2014.3001.5501

猜你喜欢

转载自blog.csdn.net/DALEONE/article/details/124389915