How to connect a Button to a task in PyQt5

 In this tutorial, we are going to talk about how we connect a button in PyQt5 to a specific task. If you don't know about PyQt5 buttons or their functionalities, you can get all the knowledge about it by reading the How to Create Buttons In PyQt5 and Animate them using Image and CSS  post. 

I'm using the "self.my_button.clicked.connect(self.my_task)" command to make a connection between my task and button. Also, I put my task under a def function as below and connect to my def function when the button is pressed using the "clicked.connect()" function.

def my_task(self):
      print("we successfully connected the Button.")
      print("You can put any of your tasks under the def function")

  in my def function, I set a print text to confirm that button is pressed. 
Note: You need to keep in mind that you need to put "self" inside the def function you created. "def my_task(self)"


If you see an output like the below if you follow my steps. 

There are a few other functions like toggled.connet()  and more. I'll make a post about them in the future. If you have a problem regarding this problem you can comment on them below. 

Post a Comment

0 Comments