pyqt5
先附上完成的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 os
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
class 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.fileName
def pbClicked2(self):
os.system('python test2.py')
就是点这个第二个按钮,然后运行test2.py文件,然后在test2.py文件中能够获取到第一个按钮打开文件返回的文件路径
下面是test2.py
import Qtdesigner.untitled as ts
from Qtdesigner.untitled import Ui_MainWindow
filename = Ui_MainWindow.pbClicked()
print(filename)
但是这个Ui_MainWindow.pbClicked(),报错Parameter 'self' unfilled