天天看点

Python+Pyqt5 文件选择对话框

办公类应用软件都有打开文件或文件夹的功能,如下图:

Python+Pyqt5 文件选择对话框

让我们使用pyqt5来实现文件选择:

首先使用可视化设计工具QTdesigner来创建一个应用窗口并添加一个菜单栏,添加一个菜单选项fileopen。当然大神可以直接敲代码,但是大神还会来看这个文章吗?

使用指令pyuic5 -o filename.py filename.ui (filename为你命名的文件名)来将.ui设计文件转化为.py文件

Python+Pyqt5 文件选择对话框

 在目录下就可以看到对应设计文件的Python文件,打开后源代码如下:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'fileopen.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        self.menufile = QtWidgets.QMenu(self.menubar)
        self.menufile.setObjectName("menufile")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionfileopen = QtWidgets.QAction(MainWindow)
        self.actionfileopen.setObjectName("actionfileopen")
        self.menufile.addAction(self.actionfileopen)
        self.menubar.addAction(self.menufile.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menufile.setTitle(_translate("MainWindow", "file"))
        self.actionfileopen.setText(_translate("MainWindow", "fileopen..."))
           

 现在,新建一个CallMainWindow.py的Python文件来实现逻辑功能,虽然可以在上面的文件中直接实现,但是将界面设计与逻辑相分离,使得代码结构清晰很多。

在文件中添加如下代码:

from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from fileopen import Ui_MainWindow
import sys
import os

class MainForm(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainForm, self).__init__()
        self.setupUi(self)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    win = MainForm()
    win.show()
    sys.exit(app.exec_())
           

 运行后,结果如下:

Python+Pyqt5 文件选择对话框

pyqt5提供了文件读取对话框的实现方法QtWidgets.QFileDialog,其中getOpenFileName方法可以获得文件名和文件类型,调用方法如下,这是菜单选项事件要调用的函数,因为没有实际应用,所以直接打印出文件名:

def open_file(self):
        fileName,fileType = QtWidgets.QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(), 
        "All Files(*);;Text Files(*.txt)")
        print(fileName)
        print(fileType)
           

在UI设计文件中可以看到,菜单选项组件的名字是actionfileopen,所以我们为他添加响应事件:

在初始化函数中添加:

self.actionfileopen.triggered.connect(self.open_file)
           

 和上述函数,结果为:

class MainForm(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainForm, self).__init__()
        self.setupUi(self)
        self.actionfileopen.triggered.connect(self.open_file)

    def open_file(self):
        fileName,fileType = QtWidgets.QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(), 
        "All Files(*);;Text Files(*.txt)")
        print(fileName)
        print(fileType)
           

这样就实现了,文件的打开,结果如下:

Python+Pyqt5 文件选择对话框
C:/Users/.../Desktop/fileopen/fileopen.py
All Files(*)