Ali
Ali

Reputation: 303

PyQt runs button functions on startup

I created a UI using Qt Designer for a python application.

The main function of my application runs when a button is clicked, like this:

self.button.clicked.connect(get_data(arg1, arg2, arg3))

To create the UI when the program begins, here is my main method:

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QDialog()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)

However, the function "get_data" runs immediately upon start-up, despite the button not even rendering on my screen. How can I stop the function from running immediately upon start-up?

Upvotes: 0

Views: 2346

Answers (1)

Mattwmaster58
Mattwmaster58

Reputation: 2579

It's because you are trying to connect the clicked event to the result of the function get_data. What you want to do is connect it to the actual function, like this:

self.button.clicked.connect(get_data)

You need to find another way to pass args into get_data. A lambda would work, but I'd use other methods (ie making a class)

Upvotes: 2

Related Questions