Attach completed untiled.py Code
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'untitled.ui'## Created by: PyQt5 UI code generator 5.13.0## WARNING! All changes made in this file will be lost!import osfrom PyQt5 import QtCore, QtGui, QtWidgetsimport sysfrom PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialogclass Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(340, 220, 93, 28)) self.pushButton.setObjectName("pushButton") self.pushButton2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton2.setGeometry(QtCore.QRect(340, 250, 93, 28)) self.pushButton2.setObjectName("pushButton2") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) self.pushButton.clicked.connect(self.pbClicked) self.pushButton2.clicked.connect(self.pbClicked2) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "PushButton")) self.pushButton2.setText(_translate("MainWindow", "PushButton2")) def pbClicked(self): self.fileName = QFileDialog.getOpenFileNames(None, " Please select the file to add ", "E:\Program Files (x86)\PycharmProjects\nn\wangyiyun", "Text Files (*.xls);;All Files (*)") return self.fileName def pbClicked2(self): os.system('python test2.py')if __name__=='__main__': app=QApplication(sys.argv) mainWindow=QMainWindow() ui=Ui_MainWindow() # You need to change your name ui.setupUi(mainWindow) mainWindow.show() sys.exit(app.exec_())
def pbClicked(self): self.fileName = QFileDialog.getOpenFileNames(None, " Please select the file to add ", "E:\Program Files (x86)\PycharmProjects\nn\wangyiyun", "Text Files (*.xls);;All Files (*)") return self.fileName
I was thinking about this filename This variable can be passed to another py In the document , And can output
def pbClicked(self): self.fileName = QFileDialog.getOpenFileNames(None, " Please select the file to add ", "E:\Program Files (x86)\PycharmProjects\nn\wangyiyun", "Text Files (*.xls);;All Files (*)") return self.fileNamedef pbClicked2(self): os.system('python test2.py')
Just click the second button , And then run test2.py file , And then in test2.py In the file, you can get the file path returned by the first button to open the file
Here is test2.py
import Qtdesigner.untitled as tsfrom Qtdesigner.untitled import Ui_MainWindowfilename = Ui_MainWindow.pbClicked()print(filename)
But this Ui_MainWindow.pbClicked(), Report errors Parameter 'self' unfilled