QFileDialog and QPixmap
-
Hi , I'm the newer one about Python.
I'm happy to write here about my python issue.
I'm trying to open file and load image,i did something to let it works but i had the syntax error.
What can i do for browsing an image in directory and load it into ?code# from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QFileDialog,QLabel,QWidget from PyQt5.QtGui import QPixmap class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(170, 100, 491, 351)) self.label.setText("") self.label.setPixmap(QPixmap("../../../../../Desktop/Tarih/Native Americans/Flag_of_the_Oneida_Nation_of_Wisconsin.png")) self.label.setObjectName("label") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21)) self.menubar.setObjectName("menubar") self.menuFile = QtWidgets.QMenu(self.menubar) self.menuFile.setObjectName("menuFile") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.actionOpen = QtWidgets.QAction(MainWindow) self.actionOpen.setObjectName("actionOpen") self.menuFile.addAction(self.actionOpen) self.menubar.addAction(self.menuFile.menuAction()) self.retranslateUi(MainWindow) self.actionOpen.triggered.connect(self.label.show) # type: ignore QtCore.QMetaObject.connectSlotsByName(MainWindow) self.actionOpen.triggered.connect(self.OpenFile) self.le = QLabel def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.menuFile.setTitle(_translate("MainWindow", "File ")) self.actionOpen.setText(_translate("MainWindow", "Open")) def OpenFile(self): options=QFileDialog.Options fname = QFileDialog.getOpenFileName(self, 'Open file','c:\\',"Image files (*.jpg *.png)",options=options) self.le.setPixmap(QPixmap(fname)) 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_()) _text
I need to work on image process but i already didn't load an image .
-
@Kurtan said in QFileDialog and QPixmap:
i had the syntax error
Please post the error and in which line it occurs
-
@jsulm This is a syntax Type-Error :
File "C:\Users\user\AppData\Local\Programs\Python\pyqt tutorial\Open.py", line 58, in OpenFile fname = QFileDialog.getOpenFileName(self, 'Open file','c:\\',"Image files (*.jpg *.png)",options=options) TypeError: getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0): argument 1 has unexpected type 'Ui_MainWindow'
-
@Kurtan said in QFileDialog and QPixmap:
fname = QFileDialog.getOpenFileName(self,
First argument is wrong: it has to be QWidget or derived class. You are passing Ui_MainWindow which is not derived from QWidget. Either pass None or something derived from QWidget.
-
Hi and welcome to devnet,
If you want to do things cleanly, you should make MainWindow a class and move your OpenFile method there.
On a side note, you should follow either the Qt or the Python coding guidelines, it will make your code easier to read and follow.
-
Also, don't modify the generated file directly and import it, otherwise next time you use
uic
again, your changed will be overwritten https://doc.qt.io/qtforpython/tutorials/basictutorial/uifiles.html#option-a-generating-a-python-class