使用QFileDialog选择一个目录并将其导入python脚本

2024-05-15 23:18:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的很茫然。我的设置如下:我使用QtDesigner创建一个GUI并得到一个ui.py。我得到了一个包装器,在这里我对信号进行了修改,称为ui_module.py。然后是我的主程序,在这里我导入ui_module.py。在这个主程序中,我希望目录的字符串可以在没有任何GUI的情况下进一步使用它(保存一些文件)。在

以下是MWE:

# ui.py
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(328, 269)

        # create a button and a label where the file path is displayed
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.savepathButton = QtGui.QPushButton(Form)
        self.savepathButton.setObjectName(_fromUtf8("savepathButton"))
        self.savepath_label = QtGui.QLabel(Form)
        self.savepath_label.setObjectName(_fromUtf8("savepath_label"))
        self.verticalLayout.addWidget(savepathButton)
        self.verticalLayout.addWidget(savepath_label)

这是包装:

^{pr2}$

下面是主程序:

import ui_module

app = ui_module.AppWindow()
    app.show()
    result = app.exec_()

    if result == 1:
        # how do I get the string of the filepath here?

我不知道在哪里最好地指定QFileDialog信号,以及如何将实际字符串携带到主程序中。如果可能的话,我想避免修改用户界面,因为这是我从QtDesigner得到的文件。在

谢谢你的帮助!在


Tags: thepyselfformappuilabelmodule