python-3.x - 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_())
解决方案
尝试这个:
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_())
推荐阅读
- python - 如何更改 argparse 中 nargs=* 的元变量值?
- python - 如何从扫描仪获取价值而不在屏幕上打印
- polymer - 在构造函数中初始化的对象与属性之间有什么区别
- java - 如何生成哈希签名?
- c# - 如果我想在一分钟内将数据从数据库写入 .txt 文件,每次都需要打开和关闭数据库连接吗?
- java - 多数据源回退
- javascript - 使用路由路由器 v4 强制更新相同的路由
- java - Apache Spark - Spark-提交 ClassNotFoundException
- angular - 在Angular组件中动态添加@Output?
- vue.js - Vue Cli 混淆的 ESLint 错误