-1

我在使用 PyQt5 时遇到了一些麻烦。我正在尝试创建一个浏览文件的程序,当找到文件时,它将路径保存在内存中,当我按下按钮时,它使用浏览路径运行另一个 .py 文件。这是我到目前为止所能做的。

文件1.py

    from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_DE(object):
    def setupUi(self, DE):
        DE.setObjectName("DE")
        DE.resize(1149, 517)
        font = QtGui.QFont()
        font.setPointSize(10)
        DE.setFont(font)

        # The "browse" button
        self.GermanyBrowse = QtWidgets.QPushButton(DE)
        self.GermanyBrowse.setGeometry(QtCore.QRect(240, 60, 91, 21))
        self.GermanyBrowse.setObjectName("GermanyBrowse")
        self.GermanyBrowse.clicked.connect(self.getfile)

        # The run button
        self.Run = QtWidgets.QPushButton(DE)
        self.Run.setGeometry(QtCore.QRect(500, 460, 156, 23))
        self.Run.setObjectName("Run")
        self.Run.clicked.connect(self.runFile())


        self.retranslateUi(DE)

    def retranslateUi(self, DE):
        _translate = QtCore.QCoreApplication.translate
        DE.setWindowTitle(_translate("DE", "Title"))
        self.Run.setText(_translate("DE", "Run"))
        self.GermanyBrowse.setText(_translate("DE", "Browse"))

    def getfile(self):
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
            None,
            "Select file",
            "",
            "*.xlsx", )
        return fileName

    def runFile(self):
        import File2.py
        file2.name_of_file()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    DE = QtWidgets.QDialog()
    ui = Ui_DE()
    ui.setupUi(DE)
    DE.show()
    sys.exit(app.exec_())

文件2.py

import File1

x = File1.Ui_DE.getfile(GermanyBrowse)


def name_of_file(str):
    alert = QtWidgets.QMessageBox()
    alert.setText(f"You have opnened {str}")
    alert.exec_()

“NameError: name 'GermanyBrowse' is not defined” - 是我运行 File2.py 时得到的

我的问题是,如何将“self.GermanyBrowse.clicked.connect(self.getfile) from File1.py 到 File2.py 的路径作为字符串调用。

谢谢!

4

1 回答 1

1

尝试一下:

文件1.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_DE(object):
    def setupUi(self, DE):
        DE.setObjectName("DE")
        DE.resize(1149, 517)
        font = QtGui.QFont()
        font.setPointSize(10)
        DE.setFont(font)

        # The "browse" button
        self.GermanyBrowse = QtWidgets.QPushButton(DE)
        self.GermanyBrowse.setGeometry(QtCore.QRect(240, 60, 91, 21))
        self.GermanyBrowse.setObjectName("GermanyBrowse")
        self.GermanyBrowse.clicked.connect(self.getfile)

        # The run button
        self.Run = QtWidgets.QPushButton(DE)
        self.Run.setGeometry(QtCore.QRect(500, 460, 156, 23))
        self.Run.setObjectName("Run")
        self.Run.clicked.connect(self.runFile)  # - ()

        self.retranslateUi(DE)

    def retranslateUi(self, DE):
        _translate = QtCore.QCoreApplication.translate
        DE.setWindowTitle(_translate("DE", "Title"))
        self.Run.setText(_translate("DE", "Run"))
        self.GermanyBrowse.setText(_translate("DE", "Browse"))

    def getfile(self):
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
            None,
            "Select file",
            "",
            "*.xlsx", )
        return fileName

    def runFile(self):
        import file2                                      # --- File2.py
        file2.name_of_file(file2.x)                       # +++ (file2.x)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    DE = QtWidgets.QDialog()
    ui = Ui_DE()
    ui.setupUi(DE)
    DE.show()
    sys.exit(app.exec_())

文件2.py

from PyQt5 import QtWidgets                               # +++
import file1                                              # - DailyMCI, + file1

#x = DailyMCI.Ui_DE.getfile(GermanyBrowse)                # ---
x = file1.Ui_DE.getfile(file1.Ui_DE)                      # +++

def name_of_file(str):
    alert = QtWidgets.QMessageBox()
    alert.setText("You have opnened {str}".format(str=str)) # I have python 3.5 (f"You have opnened {str}")
    alert.exec_()

在此处输入图像描述

于 2019-01-25T12:42:43.370 回答