gpt4 book ai didi

python-3.x - PySide2 v5.12 : Creating a FileDialog on a click of a button

转载 作者:行者123 更新时间:2023-12-04 11:47:35 31 4
gpt4 key购买 nike

我在 QtDesigner 中设计了一个基本的 UI。现在我试图通过单击按钮弹出一个简单的文件对话框,下面给出的是我的 GUI 代码:

from PySide2 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
def setupUi(self, MainWindow):
.....
.....

self.input_Image_GraphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.input_Image_GraphicsView.setGeometry(QtCore.QRect(730, 110, 480, 320))
self.input_Image_GraphicsView.setObjectName("input_Image_GraphicsView")
......
self.output_Image_GraphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.output_Image_GraphicsView.setGeometry(QtCore.QRect(730, 480, 480, 320))
self.output_Image_GraphicsView.setObjectName("output_Image_GraphicsView")
......
self.file_Select_Btn = QtWidgets.QPushButton(self.centralwidget)
self.file_Select_Btn.setGeometry(QtCore.QRect(1082, 80, 121, 28))
self.file_Select_Btn.setObjectName("file_Select_Btn")
self.file_Select_Btn.clicked.connect(self.selectFile)
.....
.....
def selectFile():
self.path_To_File = QtWidgets.QFileDialog.getOpenFileName(self, QtCore.QObject.tr("Load Image"), QtCore.QObject.tr("~/Desktop/"), QtCore.QObject.tr("Images (*.jpg)"))
print(self.path_To_File)
.....
.....

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

这是我得到的错误:
TypeError: descriptor 'tr' requires a 'PySide2.QtCore.QObject' object but received a 'str'

当我不使用“tr”时,我得到:
TypeError: 'PySide2.QtWidgets.QFileDialog.getOpenFileName' called with wrong 
argument types:
PySide2.QtWidgets.QFileDialog.getOpenFileName(Ui_MainWindow, str, str, str)
Supported signatures:
PySide2.QtWidgets.QFileDialog.getOpenFileName(PySide2.QtWidgets.QWidget = None, str = '', str = '', str = '', str = '', PySide2.QtWidgets.QFileDialog.Options = Default(QFileDialog.Options))

我已经阅读了此处给出的 Qt ver 5.12 的 python 文档: https://doc.qt.io/qtforpython/PySide2/QtWidgets/QFileDialog.html

那也没有帮助。我哪里做错了??

基本上我想:
  • 获取文件对话框 -> 选择一个 JPG 文件
  • 在 python 代码中获取文件的路径 -> 在 GUI 上使用图像填充 GraphicsView

  • 我目前在这两个方面都在苦苦挣扎

    任何帮助将不胜感激..

    最佳答案

    你不应该将你的程序逻辑与生成的 UI 文件混合在一起,而是创建一个 QMainWindow类包装器并继承自 QMainWindow和 UI 类。

    根据您的实际问题,在传递文本进行翻译之前,您只是缺少对对象的引用(例如,self),我在以下示例中添加了一个小的辅助方法来处理它:

    import sys
    from PySide2 import QtCore, QtGui, QtWidgets
    from PySide2.QtCore import QObject, QRectF, Qt
    from PySide2.QtWidgets import QMainWindow, QFileDialog, QWidget, QVBoxLayout, QGraphicsScene, QGraphicsView
    from PySide2.QtGui import QPixmap


    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    self.centralWidget = QtWidgets.QWidget(MainWindow)
    self.gridLayout = QtWidgets.QGridLayout(self.centralWidget)
    self.file_Select_Btn = QtWidgets.QPushButton(self.centralWidget)
    self.file_Select_Btn.setGeometry(QtCore.QRect(1082, 80, 121, 28))
    self.file_Select_Btn.setObjectName("file_Select_Btn")
    self.file_Select_Btn.setText("Load Image")
    self.gridLayout.addWidget(self.file_Select_Btn)
    MainWindow.setCentralWidget(self.centralWidget)

    QtCore.QMetaObject.connectSlotsByName(MainWindow)


    class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
    Ui_MainWindow.__init__(self)
    QMainWindow.__init__(self)

    # Initialize UI
    self.setupUi(self)
    self.file_Select_Btn.clicked.connect(self.showImage)

    def tr(self, text):
    return QObject.tr(self, text)

    def showImage(self):
    path_to_file, _ = QFileDialog.getOpenFileName(self, self.tr("Load Image"), self.tr("~/Desktop/"), self.tr("Images (*.jpg)"))

    self.image_viewer = ImageViewer(path_to_file)
    self.image_viewer.show()


    class ImageViewer(QWidget):
    def __init__(self, image_path):
    super().__init__()

    self.scene = QGraphicsScene()
    self.view = QGraphicsView(self.scene)
    layout = QVBoxLayout()
    layout.addWidget(self.view)
    self.setLayout(layout)

    self.load_image(image_path)

    def load_image(self, image_path):
    pixmap = QPixmap(image_path)
    self.scene.addPixmap(pixmap)
    self.view.fitInView(QRectF(0, 0, pixmap.width(), pixmap.height()), Qt.KeepAspectRatio)
    self.scene.update()


    if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

    关于python-3.x - PySide2 v5.12 : Creating a FileDialog on a click of a button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55333642/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com