In the third lesson of Python PyQt5 we are going to learn about Python PyQt5 Signal And Slots. Signal and Slots are used for communication between some objects. a Signal is emitted when a particular event occurs, and a Slot is called when its connected signal is emitted. so now we are going to create an example of Python PyQt5 Signal and Slots.
These are some imports that we need for this lesson, basically we are using the codes from the previous lesson as we have created our QPushButton with colors and styles.
1 2 3 |
from PyQt5.QtWidgets import QApplication,QWidget, QPushButton import sys from PyQt5.QtGui import QIcon |
Using this code we can set the geometry for the window. you need to add the x, y position of the window, width and height of the window.
1 |
self.setGeometry(200,200, 400,300) |
For setting the window title you need to use this line of code.
1 |
self.setWindowTitle("Button - Geekscoders.com") |
If you want to add an icon in your window, you can use this code, make sure that you have already added an icon in your working directory.
1 |
self.setWindowIcon(QIcon('python.png')) |
So you can create pyqt5 buttons by creating the object of QPushButton.
1 |
btn1 = QPushButton("Click Me", self) |
For button there is a signal that is called clicked signal, so you need to connect this clicked signal with method that you want to create. basically by clicking the button we want to just print the text in the console.
1 |
btn1.clicked.connect(self.clicked_btn) |
So you can see we have connected the clicked signal of the button with the clicked_btn() method that we are going to create.
This is just a simple method or slot that we have already connected.
1 2 |
def clicked_btn(self): print("Button One Clicked") |
So now this is the complete code for PyQt5 Working With Signal And Slots
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
from PyQt5.QtWidgets import QApplication,QWidget, QPushButton import sys from PyQt5.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200, 400,300) self.setWindowTitle("Button - Geekscoders.com") self.setWindowIcon(QIcon('python.png')) self.create_buttons() #self.show() def create_buttons(self): btn1 = QPushButton("Click Me", self) #btn1.setText("Our Second Text") #btn1.move(200,200) btn1.setGeometry(100,100, 100,100) btn1.setIcon(QIcon("cpp.png")) btn1.setStyleSheet('color:red') btn1.setStyleSheet('background-color:green') btn1.clicked.connect(self.clicked_btn) btn2 = QPushButton("Click Two", self) btn2.setGeometry(200,100, 100,100) btn2.setIcon(QIcon("pythonicon.png")) btn2.setStyleSheet('color:yellow') btn2.setStyleSheet('background-color:purple') btn2.clicked.connect(self.second_clicked) def clicked_btn(self): print("Button One Clicked") def second_clicked(self): print("Second Button Clicked") app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) |
Run the complete code and this is the result.