用PYQT5设计Python运行界面:选择文件夹的文件并自动获取文件地址

用PYQT5设计Python程序的运行界面

  • 一、在PyQT5中自定义一个窗口
  • 二、界面功能实现

  • 如图,设计了一个界面,用于选择需要执行的文件和数据集,点击运行并执行,得到结果。


    一、在PyQT5中自定义一个窗口

    在PYQT中使用其包含的工具设计一个界面,简单即可,注意给每个框和按钮命名,方便后续操作。

    二、界面功能实现

    此时的界面没有任何功能,需要写代码来实现

    1. 将ui文件转化为py文件
    2. 对py文件中的内容进行修改

    3. 对窗口进行功能实现

    从上至下的函数依次为每个路径选择按钮,如:

    # -*- coding: utf-8 -*-
    """
    Created on Thu Aug  5 20:26:42 2021
    
    @author: stack
    """
    
    import sys
    import os
    from PyQt5.QtWidgets import *
    from PyQt5 import QtWidgets
    from APP_GEN import Ui_MainWindow
    
    import APP_GEN
    
    paths=[]
    
    def input_pp_sgy(self):
        fileName,fileType=QtWidgets.QFileDialog.getOpenFileName(None,"选取文件",os.getcwd(),"All Files(*);;Text Files(*.txt)")
        paths.append(fileName)
        ui.show_pp_sgy.setText(fileName)
        
    
    def input_ps_sgy(self):
        fileName,fileType=QtWidgets.QFileDialog.getOpenFileName(None,"选取文件",os.getcwd(),"All Files(*);;Text Files(*.txt)")
        paths.append(fileName)
        ui.show_ps_sgy.setText(fileName)
       # ps_path=fileName
        
    def input_ps1_txt(self):
        fileName,fileType=QtWidgets.QFileDialog.getOpenFileName(None,"选取文件",os.getcwd(),"All Files(*);;Text Files(*.txt)")
        paths.append(fileName)
        ui.show_ps1_txt.setText(fileName)
    
    def input_ps2_txt(self):
        fileName,fileType=QtWidgets.QFileDialog.getOpenFileName(None,"选取文件",os.getcwd(),"All Files(*);;Text Files(*.txt)")
        paths.append(fileName)
        ui.show_ps2_txt.setText(fileName)
    
    def input_pp1_txt(self):
        fileName,fileType=QtWidgets.QFileDialog.getOpenFileName(None,"选取文件",os.getcwd(),"All Files(*);;Text Files(*.txt)")
        paths.append(fileName)
        ui.show_pp1_txt.setText(fileName)
    
    def input_pp2_txt(self):
        fileName,fileType=QtWidgets.QFileDialog.getOpenFileName(None,"选取文件",os.getcwd(),"All Files(*);;Text Files(*.txt)")
        paths.append(fileName)
        ui.show_pp2_txt.setText(fileName)
    
    def Output_File(self):
        directory=QtWidgets.QFileDialog.getExistingDirectory(None, "请选择文件夹路径","D:/")
        paths.append(directory)
        ui.show_outputfile.setText(directory)
    
    def DONE(self):
        if not os.path.isfile("paths.txt"):
            f=open("paths.txt", mode="w")
        else:
            f=open("paths.txt",mode="w")
        str = '\n'
        f.write(str.join(paths))
        f.close
    
    def run_py(self):
        ui.lineEdit.setText("RUNNING")
        #print(paths)
        os.system("python final_Converse_APP.py")
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        MainWindow = QMainWindow()
        ui = APP_GEN.Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        ui.input_pp_sgy.clicked.connect(input_pp_sgy)
        ui.input_ps_sgy.clicked.connect(input_ps_sgy)
        ui.input_ps1_txt.clicked.connect(input_ps1_txt)
        ui.input_ps2_txt.clicked.connect(input_ps2_txt)
        ui.input_pp1_txt.clicked.connect(input_pp1_txt)
        ui.input_pp2_txt.clicked.connect(input_pp2_txt)
        ui.Output_File.clicked.connect(Output_File)
        ui.DONE.clicked.connect(DONE)
        ui.pushButton.clicked.connect(run_py)
        sys.exit(app.exec_())
    

    部分代码解释:

    1. 按钮与函数执行建立连接点击按钮即执行对应的函数
        ui.input_pp_sgy.clicked.connect(input_pp_sgy)
        ui.input_ps_sgy.clicked.connect(input_ps_sgy)
        ui.input_ps1_txt.clicked.connect(input_ps1_txt)
        ui.input_ps2_txt.clicked.connect(input_ps2_txt)
        ui.input_pp1_txt.clicked.connect(input_pp1_txt)
        ui.input_pp2_txt.clicked.connect(input_pp2_txt)
        ui.Output_File.clicked.connect(Output_File)
        ui.DONE.clicked.connect(DONE)
        ui.pushButton.clicked.connect(run_py)
    
    1. 将文件路径保存保存至文件夹
      先定义一个列表path=[ ],每个函数中有一条语句:paths.append(fileName),本程序中的DONE函数即是将path的内容存进一个文件夹,点击“DONE”按钮,自动生成path.txt文件。
    def DONE(self):
        if not os.path.isfile("paths.txt"):
            f=open("paths.txt", mode="w")
        else:
            f=open("paths.txt",mode="w")
        str = '\n'
        f.write(str.join(paths))
        f.close
    



    至此,功能完整实现,若需要对文件路径进行操作,可转化为对paths文件进行操作。在本程序中,对路径的操作如下:

    if __name__ == '__main__':
        file=open('paths.txt')    
        paths_list=[]
        with open('paths.txt','r') as f:
            for line in f:
                paths_list.append(line.strip('\n').split(',')[0])
        print(paths_list)
        pp_path, ps_path, ps1_path, ps2_path, pp1_path, pp2_path, outputfile=paths_list
        pp, ps = read_to_numpy(pp_path, ps_path)
    

    这样就把路径提取出来了,可以正常使用。

    来源:AStackhouze

    物联沃分享整理
    物联沃-IOTWORD物联网 » 用PYQT5设计Python运行界面:选择文件夹的文件并自动获取文件地址

    发表评论