制作一个图形界面,目的是通过运行a.py选择文件夹地址(储存到pathm),将pathm在b.py中引用,并且进行数据处理,但卡在了传递pathm这块
代码如下:
a.py:
import b
from PyQt5 import QtCore, QtWidgets
pathm = 'add here'
class Ui_mainwindow(object):
def setupUi(self, mainwindow):
# 主窗口参数设置
mainwindow.setObjectName("mainwindow")
mainwindow.resize(891, 692)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(mainwindow)
self.pushButton.setGeometry(QtCore.QRect(540, 350, 121, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(mainwindow)
self.pushButton_2.setGeometry(QtCore.QRect(230, 350, 121, 51))
self.pushButton_2.setObjectName("pushButton_2")
#self.retranslateUi(mainwindow)
self.pushButton.clicked.connect(mainwindow.close)
self.groupBox = QtWidgets.QGroupBox(mainwindow)
self.groupBox.setGeometry(QtCore.QRect(330, 50, 221, 171))
self.groupBox.setObjectName("groupBox")
self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.groupBox)
self.dateTimeEdit.setGeometry(QtCore.QRect(10, 40, 194, 22))
self.dateTimeEdit.setObjectName("dateTimeEdit")
self.dateTimeEdit_2 = QtWidgets.QDateTimeEdit(self.groupBox)
self.dateTimeEdit_2.setGeometry(QtCore.QRect(10, 100, 194, 22))
self.dateTimeEdit_2.setObjectName("dateTimeEdit_2")
self.file = QtWidgets.QPushButton(mainwindow)
self.file.setGeometry(QtCore.QRect(230, 270, 93, 28))
self.file.setObjectName("file")
self.fileT = QtWidgets.QPushButton(mainwindow)
self.fileT.setGeometry(QtCore.QRect(390, 270, 400, 28))
self.fileT.setObjectName("fileT")
mainwindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainwindow)
self.menubar.setGeometry(QtCore.QRect(40, 200, 848, 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)
################button按钮点击事件回调函数################
self.file.clicked.connect(self.msg)
def msg(self):
pathm = QtWidgets.QFileDialog.getExistingDirectory(None, "选取文件夹", "C:/") # 起始路径
self.fileT.setText(pathm)
print(pathm)
#global pathm
return pathm
#---------------------------------------------------------------------------------------#
def retranslateUi(self, mainwindow):
_translate = QtCore.QCoreApplication.translate
mainwindow.setWindowTitle(_translate("mainwindow", "数据处理"))
self.pushButton.setText(_translate("mainwindow", "退出"))
self.pushButton_2.setText(_translate("mainwindow", "数据处理"))
self.pushButton_2.clicked.connect(self.OpenClick)
self.file.setText(_translate("mainwindow", "选择文件夹"))
self.fileT.setText(_translate("mainwindow", ""))
self.groupBox.setTitle(_translate("mainwindow", " 时间选择 "))
def OpenClick(self):
algorithm.dataprocess()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_mainwindow()
ui.setupUi(MainWindow)
ui.msg()
MainWindow.show()
sys.exit(app.exec_())
b.py片段
from b import pathm
def eachFile(filepath): # 按文件夹处理数据
pathDir = os.listdir(filepath) # 文件路径
file_path_list = list()
for allDir in pathDir: # 遍历文件夹
child = os.path.join('%s%s' % (filepath, allDir))
file_path_list.append(child)
return file_path_list
print(pathm)
pathm = 'D:\\data\\数据\\'
file_path_list = eachFile(pathm) # 文件夹路径
运行结果是在点击选择文件夹后,a.py确实能打印地址,但如果启动b.py,就会打印“add here”,不知道如何解决,请各位赐教