Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help to use QFileDialog.



  • 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!



  • I got it. The problem is that a lambda function was necessary when calling the method clicked.connect

    just change the line

    self.load_data_button.clicked.connect(self.openFile())
    

    to

    self.load_data_button.clicked.connect(lambda: self.openFile())`
    

  • Lifetime Qt Champion

    @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)
    

  • Banned

    To expand on what jsulm posted -- This is a function/method reference self.openfile while this is a call a to that function/method self.openfile() when you are including a function/method in as part of a connect statement you use a reference to the function/method otherwise what you are passing is the results of immediately calling that function/method

    Also when using a function/method within a .connect(self.openfile) call you need to apply the pyqtSlot decorator to that function/method as follows:

        @pyqtSlot()
        def openfile(self):
    


  • @jsulm When I call that way the button doesn't do anything


  • Banned

    Okay here it is fixed and more python-pyqt-ish

    from PyQt5.QtCore    import pyqtSlot
    from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton
    
    class UI_MainWindow(QWidget):
        def __init__(self):
            QWidget.__init__(self)
    
            self.setWindowTitle("L Plot Beta v1.0")
            self.resize(250,75)
    
            self.btnLoadData = QPushButton('Load Data')
            self.btnLoadData.clicked.connect(self.openFile)
    
            HBox = QHBoxLayout()
            HBox.addWidget(self.btnLoadData)
            HBox.addStretch(1)
            
            VBox = QVBoxLayout()
            VBox.addLayout(HBox)
            VBox.addStretch(1)
            
            self.setLayout(VBox)
           
        @pyqtSlot()
        def openFile(self):
             print('Opening a File Now')
        
    if __name__ == '__main__':
        MainEvntHndlr = QApplication([])
    
        MainApp = UI_MainWindow()
        MainApp.show()
    
        MainEvntHndlr.exec()
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    

Log in to reply