浏览文件并在其他文件中使用其路径

时间:2019-01-25 11:59:38

标签: python oop pyqt5 python-object

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

File1.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_())

File2.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:未定义名称'GermanyBrowse'”-运行File2.py时得到的是

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

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试一下:

file1.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_())

file2.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_()

enter image description here