天天看點

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(*)