与 PyQt5 QFileDialog 混淆
Confused with PyQt5 QFileDialog
我正在尝试编写一个简单的脚本,其中包含 GUI 文件 select 对话框和 return selected 文件名到主程序代码。我在网上查找了一些想法,但我对这一切如何运作感到困惑。我有以下代码:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QMessageBox
from PyQt5.QtGui import QIcon
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Bla'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.openFileNameDialog()
self.show()
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
return(fileName)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
alert = QMessageBox()
alert.setText(f'selected file {ex}')
alert.exec_()
sys.exit(app.exec_())
我添加了一个消息框来查看文件 selection 是否工作正常,但是如果我 运行 它消息框显示:
selected file <_main_.App object at 0x0000000002EA9288>
同时,当我将消息框放入 openFileNameDialog 时,一切正常:
~snip~
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
alert = QMessageBox()
alert.setText(f'selected file {fileName}')
alert.exec_()
~snip~
所以问题是我如何 return 将文件名正确地返回到主程序?提前感谢您的帮助!
- 什么是
ex
?是代表widget的变量,不是fileName。
- 什么 returns openFileNameDialog 保存在某个变量中? 不,那么该信息丢失了。如果您想获取以某种方法创建的信息,那么另一种方法是将其保存为 class.
的属性
综合以上,解决方案是:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QMessageBox
class App(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.fileName = ""
self.initUI()
def initUI(self):
self.setWindowTitle("Bla")
self.setGeometry(10, 10, 640, 480)
self.openFileNameDialog()
self.show()
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(
self,
"QFileDialog.getOpenFileName()",
"",
"All Files (*);;Python Files (*.py)",
options=options,
)
self.fileName = fileName
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
alert = QMessageBox()
alert.setText(f"selected file {ex.fileName}")
alert.exec_()
sys.exit(app.exec_())
我正在尝试编写一个简单的脚本,其中包含 GUI 文件 select 对话框和 return selected 文件名到主程序代码。我在网上查找了一些想法,但我对这一切如何运作感到困惑。我有以下代码:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QMessageBox
from PyQt5.QtGui import QIcon
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Bla'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.openFileNameDialog()
self.show()
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
return(fileName)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
alert = QMessageBox()
alert.setText(f'selected file {ex}')
alert.exec_()
sys.exit(app.exec_())
我添加了一个消息框来查看文件 selection 是否工作正常,但是如果我 运行 它消息框显示:
selected file <_main_.App object at 0x0000000002EA9288>
同时,当我将消息框放入 openFileNameDialog 时,一切正常:
~snip~
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
alert = QMessageBox()
alert.setText(f'selected file {fileName}')
alert.exec_()
~snip~
所以问题是我如何 return 将文件名正确地返回到主程序?提前感谢您的帮助!
- 什么是
ex
?是代表widget的变量,不是fileName。 - 什么 returns openFileNameDialog 保存在某个变量中? 不,那么该信息丢失了。如果您想获取以某种方法创建的信息,那么另一种方法是将其保存为 class. 的属性
综合以上,解决方案是:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QMessageBox
class App(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.fileName = ""
self.initUI()
def initUI(self):
self.setWindowTitle("Bla")
self.setGeometry(10, 10, 640, 480)
self.openFileNameDialog()
self.show()
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(
self,
"QFileDialog.getOpenFileName()",
"",
"All Files (*);;Python Files (*.py)",
options=options,
)
self.fileName = fileName
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
alert = QMessageBox()
alert.setText(f"selected file {ex.fileName}")
alert.exec_()
sys.exit(app.exec_())