Python+Pyqt5 文件選擇對話框

辦公類應用軟件都有打開文件或文件夾的功能,如下圖:

 

讓我們使用pyqt5來實現文件選擇:

首先使用可視化設計工具QTdesigner來創建一個應用窗口並添加一個菜單欄,添加一個菜單選項fileopen。當然大神可以直接敲代碼,但是大神還會來看這個文章嗎?

使用指令pyuic5 -o filename.py filename.ui (filename爲你命名的文件名)來將.ui設計文件轉化爲.py文件

 在目錄下就可以看到對應設計文件的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_())

 運行後,結果如下:

 

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)

這樣就實現了,文件的打開,結果如下:

C:/Users/.../Desktop/fileopen/fileopen.py
All Files(*)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章