返回顶部

PyQt5 控件学习(一个一个学习之QFileDialog)

QFileDialog的继承图:

 

 

QFileDialog的描述:

 

 

 

 

QFileDialog的继承:

它继承自QDialog  

 

QFileDialog的功能作用:

QFileDialog的功能作用之最简单的获取方式(静态方法):

 

第一个方法的使用(获取单个文件):

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):                                                # 这里是过滤器
        ret =  QFileDialog.getOpenFileName(self,"选择一个py文件","./","ALL(*.*);;Images(*.png *.jpg);;Python文件(*.py)","Python文件(*.py)")
        print(ret)
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
getOpenFileName

第二个,获取多个文件:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):                                                # 这里是过滤器
        ret =  QFileDialog.getOpenFileNames(self,"选择一个py文件","./","ALL(*.*);;Images(*.png *.jpg);;Python文件(*.py)","Python文件(*.py)")
        print(ret)
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
选多个文件

 

第三个:获取文件的url 地址:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):                                                # 这里是过滤器
        ret =  QFileDialog.getOpenFileUrl(self,"选择一个py文件","./","ALL(*.*);;Images(*.png *.jpg);;Python文件(*.py)","Python文件(*.py)")
        print(ret)
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

此时得到的是个url 对象 。

 

第五个,保存时设置文件名称:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):                                                # 这里是过滤器
        ret =  QFileDialog.getSaveFileName(self,"选择一个py文件","./","ALL(*.*);;Images(*.png *.jpg);;Python文件(*.py)","Python文件(*.py)")
        print(ret)
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

下面是两个获取文件夹的方法:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        ret =  QFileDialog.getExistingDirectory(self,"选择一个py文件","./")
        print(ret)
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
getExistingDirectory

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        ret =  QFileDialog.getExistingDirectoryUrl(self,"选择一个py文件",QUrl("./"))
        print(ret)
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
getExistingDirectoryUrl

补充:过滤器的格式:

分隔是以;;  (两个分号)

其实,一般情况下,我们都是使用这上面的静态方法,简单好用。 

 

QFileDialog的功能作用之构造函数:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")

        fileDialog.open()  #此时如何拿到用户选择的文件呢,这里要借助信号了。

        fileDialog.fileSelected.connect(lambda file:print(file))



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
利用fileSelected接收用户的选择

 

 

QFileDialog的功能作用之接收模式:

这里来设置保存还是打开,

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")

        fileDialog.setAcceptMode(QFileDialog.AcceptSave)  #此时就是保存的对话框了
        

        fileDialog.open()  #此时如何拿到用户选择的文件呢,这里要借助信号了。
        fileDialog.fileSelected.connect(lambda file:print(file))



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

 

 

 

QFileDialog的功能作用之默认后缀:

 

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")

        fileDialog.setAcceptMode(QFileDialog.AcceptSave)
        fileDialog.setDefaultSuffix("txt")  #设置默认后缀名

        fileDialog.open()  #此时如何拿到用户选择的文件呢,这里要借助信号了。
        fileDialog.fileSelected.connect(lambda file:print(file))



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

 

QFileDialog的功能作用之设置文件模式:

如何区分文件和文件夹呢?

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")

        fileDialog.setFileMode(QFileDialog.Directory)

        fileDialog.open()



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

 

QFileDialog的功能作用之设置名称过滤器:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")

        # fileDialog.setFileMode(QFileDialog.Directory)

        # fileDialog.setNameFilter("IMG(*.png *.jpg)")  #会把之前的给覆盖了

        fileDialog.setNameFilters(["ALL(*.*)","Python文件(*.py)","Images(*.png *.jpg)"])
        
        fileDialog.open()



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
过滤器设置

 

QFileDialog的功能作用之设置信息的详细程度:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")


        fileDialog.setNameFilters(["ALL(*.*)","Python文件(*.py)","Images(*.png *.jpg)"])

        # fileDialog.setViewMode(QFileDialog.List)
        fileDialog.setViewMode(QFileDialog.Detail)  #和上面的差不多  


        fileDialog.open()



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

QFileDialog的功能作用之设置指定角色的标签名称:

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")
        fileDialog.setNameFilters(["ALL(*.*)","Python文件(*.py)","Images(*.png *.jpg)"])

        # fileDialog.setLabelText(QFileDialog.FileName,"我的文件")
        fileDialog.setLabelText(QFileDialog.Accept,"Sure")
        fileDialog.setLabelText(QFileDialog.Reject,"Cancel")





        fileDialog.open()



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

QFileDialog的功能作用之打开对话框:

和之前的一样。

 

 

 

QFileDialog的信号:

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")
        fileDialog.setNameFilters(["ALL(*.*)","Python文件(*.py)","Images(*.png *.jpg)"])

        fileDialog.currentChanged.connect(lambda path:print("当前路径改变",path))
        fileDialog.currentUrlChanged.connect(lambda url:print("当前Qurl改变",url))

        fileDialog.open()



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
currentChanged 和 currentUrlChanged
from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")
        fileDialog.setNameFilters(["ALL(*.*)","Python文件(*.py)","Images(*.png *.jpg)"])

        fileDialog.directoryEntered.connect(lambda path:print("进入目录",path))
        fileDialog.directoryUrlEntered.connect(lambda url:print("进入目录(QUrl)",url))

        fileDialog.open()



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
directoryEntered 和 directoryUrlEntered

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")
        fileDialog.setNameFilters(["ALL(*.*)","Python文件(*.py)","Images(*.png *.jpg)"])

        fileDialog.filterSelected.connect(lambda filter:print("过滤器被选中",filter))

        fileDialog.open()



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
filterSelected 信号

 

继续:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QFileDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):

        fileDialog = QFileDialog(self,"选择一个文件","./","ALL(*.*);;Python文件(*.py);;Images(*.png *.jpg)")
        fileDialog.setNameFilters(["ALL(*.*)","Python文件(*.py)","Images(*.png *.jpg)"])

        #这里用到前面的文件模式:
        fileDialog.setFileMode(QFileDialog.ExistingFiles)

        fileDialog.fileSelected.connect(lambda file:print("单个文件被选中- 字符串",file))
        fileDialog.filesSelected.connect(lambda file:print("多个文件被选中- 列表[字符串]",file))
        fileDialog.urlSelected.connect(lambda file:print("单个文件被选中- url ",file))
        fileDialog.urlsSelected.connect(lambda file:print("多个文件被选中- 列表[url]",file))

        fileDialog.open()



        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        pass

    def test(self):
        pass
        
        
        
        
if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
fileSelected filesSelected urlsSelected urlSelected

 

总结:

以上就是文件选择对话框,下面是输入对话框:QInputDialog:  https://www.cnblogs.com/zach0812/p/11392844.html

 

posted @ 2019-08-22 14:53  Zcb0812  阅读(1880)  评论(0编辑  收藏  举报