REAL1TY 2021-11-14 22:34 采纳率: 100%
浏览 662
已结题

pyqt5中如何将类class中的方法中的参数调用至另一个函数

制作一个图形界面,目的是通过运行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”,不知道如何解决,请各位赐教

  • 写回答

3条回答 默认 最新

  • ~白+黑 Python领域新星创作者 2021-11-15 11:08
    关注

    把pathhm变成类属性,赋予初始值,然后在b中实例化,再调用属性就可以了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月1日
  • 已采纳回答 10月24日
  • 创建了问题 11月14日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装