Help to use QFileDialog.
Solved
Qt for Python
-
Hello, i'm still learning how to use pyqt5 and i'm trying to make a plot application for myself. To plot my graphs I have to load data from a file. I did make a button for this purpose. The goal is to click the button to open a fileDialog window.
Here is a sample of the code in this regard:
from PyQt5 import QtWidgets, QtCore, QtGui, Qt class Ui_MainWindow(QtWidgets.QWidget): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.setWindowTitle("L Plot Beta v1.0") MainWindow.resize(800,600) Ui_MainWindow().loadDataButton() def loadDataButton(self): self.load_data_button = QtWidgets.QPushButton(MainWindow) self.load_data_button.setGeometry(QtCore.QRect(430, 100, 100, 30)) self.load_data_button.setText("Load Data") self.load_data_button.clicked.connect(self.openFile()) def openFile(self): fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', ' ', 'All Files (*);;Text Files (*.txt)') if fileName: print(fileName) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() Ui_MainWindow().setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
When I run this code the File Dialog box pops up before the main window. It is suppose to open the main window, then when I click the button it shows de File dialog box.
What I'm doing wrong?
Thanks in advance!
-
-
@luguecos said in Help to use QFileDialog.:
self.load_data_button.clicked.connect(self.openFile())
Here you're calling the slot. It should be:
self.load_data_button.clicked.connect(self.openFile)