In this lesson i want to show you How to Create Calculator in PyQt6.
What is PyQt6 ?
PyQt6 is set of Python bindings for the Qt6 application framework and runs on all platforms supported by Qt6. PyQt6 enables the use of Qt6 libraries in Python programs. Qt is popular cross-platform application development framework and PyQt6 allows you to use its functionality in your Python programs to create graphical user interfaces (GUIs) and perform other tasks.
What are Key Features of PyQt6 ?
PyQt6 is Python bindings for the Qt6 application framework and has the following key features:
- Cross-platform compatibility: PyQt6 works on all major platforms including Windows, macOS and Linux.
- Graphical User Interfaces (GUIs): PyQt6 makes it easy to create professional looking GUIs using its vast collection of widgets, layouts and graphics.
- Large community and support: PyQt6 has large and active community of developers and users who offer support and contribute to its development.
- Robust and efficient: PyQt6 uses the underlying Qt6 C++ library which is known for its efficiency and stability and provides Python bindings to this powerful library.
- Integration with other technologies: PyQt6 integrates easily with other technologies such as SQL databases, XML and web technologies and it makes it best and flexible tool for developing different types of applications.
- Third-party plugins: PyQt6 supports a variety of third-party plugins and extensions, allowing developers to extend its functionality and take advantage of other tools and libraries.
- Extensive documentation: PyQt6 comes with comprehensive documentation, making it easy for developers to get started and learn how to use its features and functions.
How to Install PyQt6 ?
You can install PyQt6 using pip.
pip3 install PyQt6
Difference between PyQt5 and PyQt6
PyQt5 and PyQt6 are both Python bindings for the Qt application framework. The main difference between the two is that PyQt5 is based on Qt5, while PyQt6 is based on Qt6.
- Qt Versions: PyQt5 is based on Qt5, while PyQt6 is based on Qt6. Qt6 is the latest version of the Qt framework and comes with several new features, improvements, and bug fixes compared to Qt5.
- API Changes: PyQt6 has some API changes compared to PyQt5 due to the changes in the underlying Qt framework. This means that code written for PyQt5 may need to be updated to work with PyQt6.
- Performance: PyQt6 is expected to be faster and more efficient than PyQt5 due to the improvements in the underlying Qt framework.
- Availability: PyQt5 is more widely used and has more support and resources available compared to PyQt6, which is still a relatively new addition to the PyQt family.
In summary, if you are starting new project and have the option to choose between PyQt5 and PyQt6, it is recommended to use PyQt6 due to its improved features and performance. if you have an existing project based on PyQt5, it may not be worth to upgrade to PyQt6 unless you need the new features or improvements offered by the latest version of the Qt framework.
How to Create Calculator in PyQt6 ?
This is example for creating a basic calculator.
from PyQt6.QtWidgets import QApplication, QWidget, QGridLayout, QLineEdit, QPushButton
grid = QGridLayout()
self.display = QLineEdit()
grid.addWidget(self.display, 0, 0, 1, 4)
names = ['7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+']
positions = [(i, j) for i in range(1, 5) for j in range(4)]
for position, name in zip(positions, names):
if name == '=':
btn = QPushButton(name)
btn = QPushButton(name)
btn.clicked.connect(lambda x, b=name: self.append_number(b))
def append_number(self, b):
self.display.setText(self.display.text() + b)
result = eval(self.display.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
calculator = Calculator()
The above code is simple calculator program that is written in Python using PyQt6 library. in the code we have
Calculator class that creates calculator widget with grid layout that has display line edit and buttons for the numbers and operations. the
initUI method creates the calculator grid layout, sets up the display line edit, adds buttons to the grid and sets the window title.
append_number method is connected to the number buttons and updates the display line edit with corresponding number when the button is clicked. the
equals method evaluates the expression in the display line edit and updates the display line edit with the result or shows an error message if the evaluation fails.
The code creates an instance of the
Calculator class and launches the calculator widget. The
sys.exit function ensures that the application terminates cleanly.
Run the complete code and this will be the result.
Learn More on Python
- PyQt6: The Ultimate GUI Toolkit for Python
- Python: The Most Versatile Programming Language of the 21st Century
- Tkinter: A Beginner’s Guide to Building GUI Applications in Python
- PySide6: The Cross-Platform GUI Framework for Python
- The Ultimate Guide to Kivy: Building Cross-Platform Apps with Python
- Discover the Power of Django: The Best Web Framework for Your Next Project
- How to Earn Money with Python
- Why Flask is the Ideal Micro-Web Framework
- Python Pillow: The Ultimate Guide to Image Processing with Python
- Get Started with Pygame: A Beginner’s Guide to Game Development with Python
- Python PyOpenGL: A Guide to High-Performance 3D Graphics in Python
- The Cross-Platform Game Development Library in Python
- Unleash the Power of Computer Vision with Python OpenCV
- Unleash the Power of Automated Testing with Python Selenium