PyQt5 is a set of Python bindings for the cross-platform application framework that combines all the advantages of Qt and Python. With PyQt5, you can include Qt libraries in Python code, enabling you to write GUI applications in Python. In other words, PyQt allows you to access all the facilities provided by Qt through Python code. Since PyQt5 depends on the Qt libraries to
Before starting please ensure that you have installed PyQt5 or install it by running this command.
sudo pip3 install PyQt5
It will install PyQt5 libraries as well as the PyQt5-sip library.
PyQt5 Push Button
To display a push button in an application, you need to create an instance of the QPushButton class. When assigning text to buttons, you can create shortcut keys by preceding any character in the text with an ampersand. For example, if the text assigned to a push button is Click Me, the character C will be underlined to indicate that it is a shortcut key, and the user can select the button by pressing Alt + C. The button emits the clicked() signal if it is activated. Besides text, an icon can also be displayed in the push button. The methods for displaying text and an icon in a push button are as follows:
- setText(
) : This method is used to assign text to the push button - setIcon(
) : This method is used to assign an icon to the push button
So Let’s code it. First we will import all the needed libraries.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
Then we will make a PushButton class and override its constructors. We will also set the function which the button will perform when pressed.
class PushButton(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 Push button - pyblog.in'
self.width = 400
self.height = 300
self.left = 10
self.top = 10
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example pyqt5 push button')
button.move(150,100)
button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
print('PyQt5 pushbutton is clicked')
Lastly we will create an object of the class and run it.
if __name__ == '__main__':
app = QApplication(sys.argv)
button = PushButton()
sys.exit(app.exec_())
Run using
python3 button.py
Output
How it worked?
- It imports the necessary modules. QWidget is the base class of all user interface objects in PyQt5.
- It creates a new PushButton class that inherits from the base class, QWidget.
- It provides the default constructor for QWidget. The default constructor has no parent, and a widget with no parent is known as a window.
- It creates an application object with the name app through the QApplication() method. Every PyQt5 application must create sys.argv application object which contains a list of arguments from the command line, and it is passed to the method while creating the application object. The sys.argv parameter helps in passing and controlling the startup attributes of a script.
- An instance of the PushButton class is created with the name window.
- The click on the button activates the on_click function and performs the action in it.
- The show() method will display the widget on the screen.
- The sys.exit() method ensures a clean exit, releasing memory resources.
Please Note that The exec_() method has an underscore because exec is a Python keyword.
Code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class PushButton(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 Push button - pyblog.in'
self.width = 400
self.height = 300
self.left = 10
self.top = 10
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example pyqt5 push button')
button.move(150,100)
button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
print('PyQt5 pushbutton is clicked')
if __name__ == '__main__':
app = QApplication(sys.argv)
button = PushButton()
sys.exit(app.exec_())
bookmarked!!, I really like your site!