先附上完成的untiled.py代碼
# -*- 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, "請選擇要添加的文件", "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() #需要改名 ui.setupUi(mainWindow) mainWindow.show() sys.exit(app.exec_())
def pbClicked(self): self.fileName = QFileDialog.getOpenFileNames(None, "請選擇要添加的文件", "E:\Program Files (x86)\PycharmProjects\nn\wangyiyun", "Text Files (*.xls);;All Files (*)") return self.fileName
我想的是這個filename這個變量能夠傳到另一個py文件裡面,並且能夠輸出
def pbClicked(self): self.fileName = QFileDialog.getOpenFileNames(None, "請選擇要添加的文件", "E:\Program Files (x86)\PycharmProjects\nn\wangyiyun", "Text Files (*.xls);;All Files (*)") return self.fileNamedef pbClicked2(self): os.system('python test2.py')
就是點這個第二個按鈕,然後運行test2.py文件,然後在test2.py文件中能夠獲取到第一個按鈕打開文件返回的文件路徑
下面是test2.py
import Qtdesigner.untitled as tsfrom Qtdesigner.untitled import Ui_MainWindowfilename = Ui_MainWindow.pbClicked()print(filename)
但是這個Ui_MainWindow.pbClicked(),報錯Parameter 'self' unfilled