1

I'm trying to implement a file browser by clicking on a button. My current problem is that when I run this code, the file browser automatically comes up.

The relevant parts are:

def myChanges(self, MainWindow):
    self.pushButton.connect(self.selectFile())

and

def selectFile(self):
    filePath = QtGui.QFileDialog.getOpenFileName()
    print(filePath)

My Questions are:

  1. How do you make it so that the file dialog only comes up after clicking on the button?
  2. Does self.pushButton.toggled.connect(self.selectFile()) or self.pushButton.clicked.connect(self.selectFile()) do anything differently?
  3. Does passing MainWindow into selectFile do anything differently?

Full code:

from PySide import QtCore, QtGui

class Ui_MainWindow(object):
    def selectFile(self, MainWindow):
        filePath = QtGui.QFileDialog.getOpenFileName()
        print(filePath)

    def myChanges(self, MainWindow):
        self.pushButton.connect(self.selectFile())

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 40, 75, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.myChanges(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Arda Arslan
  • 1,331
  • 13
  • 24

1 Answers1

3
  1. The problem is generated by the wrong connection. The way of connection is as follows:

{sender}.{your_signal}.connect({your_slot})

In your case it should be self.pushButton.clicked.connect(self.selectFile), for this you must change:

def myChanges(self, MainWindow):
    self.pushButton.connect(self.selectFile())

to:

def myChanges(self, MainWindow):
    self.pushButton.clicked.connect(self.selectFile)
  1. According to the documentation:

void QAbstractButton::toggled(bool checked)

This signal is emitted whenever a checkable button changes its state. checked is true if the button is checked, or false if the button is unchecked.

void QAbstractButton::clicked(bool checked = false)

This signal is emitted when the button is activated (i.e., pressed down then released while the mouse cursor is inside the button), when the shortcut key is typed, or when click() or animateClick() is called. Notably, this signal is not emitted if you call setDown(), setChecked() or toggle().

In the first case you must enable the checkeable with:

self.pushButton.setCheckable(True)

In the case of toggled this gives you a more called isChecked attribute that is alternating between True and False. For your problem it is not necessary.

  1. It is not necessary to pass the parameter MainWindow but you will use it. In your case you could change it to:

def selectFile(self):
    filePath = QtGui.QFileDialog.getOpenFileName()
    print(filePath)

def myChanges(self):
    self.pushButton.clicked.connect(self.selectFile)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241