首页 > 解决方案 > python - 如何从使用PyQt5单击时关闭的小部件传递字符串?

问题描述

我制作了一个小部件,提示用户浏览并选择文件路径。我想复制此文件路径字符串,以便在第一个小部件关闭后可以保留该字符串。通过这种方式,我可以将此字符串传递给我尚未编码的其他小部件。

下面的代码片段将成功提示用户,并将通过 gui 显示正确的文件路径。但是,print语句输出None而不是文件路径字符串。该print语句指的是行print("\n .. FPATH:\n{}\n".format(file_selection_widget.fpath))我的问题是:为什么会这样?我尝试self.close()从内部范围更改file_selection_widget.close()为外部范围(in __main__),但这会导致用户提示失败;我不确定这里还有什么问题。

import sys
from PyQt5 import QtWidgets #, QtGui, QtCore

class FileSelectionWidget(QtWidgets.QWidget):

    """
    This class allows the user to select the input
    data file to be read via gui.
    """

    def __init__(self, parent=None):
        self._fpath = None
        super().__init__(parent)
        button = QtWidgets.QPushButton("Click here and select the data file you want to read")
        button.clicked.connect(self.on_clicked)
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(button)

    @property
    def fpath(self):
        return self._fpath

    def on_clicked(self):
        self.prompt_input_file()
        self.prompt_verification()

    def prompt_input_file(self):
        fpath, _ = QtWidgets.QFileDialog.getOpenFileName(
            None, 'Select input file', '/home', '',
            options=QtWidgets.QFileDialog.DontUseNativeDialog)
        self._fpath = fpath
        self.close()

    def prompt_verification(self):
        alert = QtWidgets.QMessageBox()
        alert.setText('The input filepath you selected is: \n{}'.format(self.fpath)) # shows correct filepath
        alert.exec()

if __name__ == '__main__':

    ## initialize application
    app = QtWidgets.QApplication(sys.argv)

    ## select path of input data file
    file_selection_widget = FileSelectionWidget()
    file_selection_widget.show()

    print("\n .. FPATH:\n{}\n".format(file_selection_widget.fpath)) ## shows incorrect filepath

    ## ... back end loads data file and processes data
    ## ... more gui

    ## exit application
    sys.exit(app.exec_())

标签: python-3.xoopuser-interfacepyqt5widget

解决方案


尝试这个:

import sys
from PyQt5 import QtWidgets #, QtGui, QtCore

class FileSelectionWidget(QtWidgets.QWidget):

    """
    This class allows the user to select the input
    data file to be read via gui.
    """

    def __init__(self, parent=None):
        self._fpath = None
        super().__init__(parent)
        button = QtWidgets.QPushButton("Click here and select the data file you want to read")
        button.clicked.connect(self.on_clicked)
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(button)

    @property
    def fpath(self):
        return self._fpath

    def on_clicked(self):
        self.prompt_input_file()
        self.prompt_verification()

    def prompt_input_file(self):
        fpath, _ = QtWidgets.QFileDialog.getOpenFileName(
            None, 'Select input file', '/home', '',
            options=QtWidgets.QFileDialog.DontUseNativeDialog)
        self._fpath = fpath
        print("Your Selected Path :\n{}\n".format(file_selection_widget.fpath)) ## shows incorrect filepath
        self.close()
        

    def prompt_verification(self):
        alert = QtWidgets.QMessageBox()
        alert.setText('The input filepath you selected is: \n{}'.format(self.fpath)) # shows correct filepath
        alert.exec()

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    file_selection_widget = FileSelectionWidget()
    file_selection_widget.show()
    sys.exit(app.exec_())

推荐阅读