In this tutorial, we will be focusing on creating a simple graphical user interface (GUI) using PyQt. PyQt is a set of Python bindings for the Qt application framework and runs on all platforms supported by Qt including Windows, macOS, Linux, Android, and iOS. PyQt allows you to build both simple and complex applications with ease.
In this first part of the tutorial, we will cover the following topics:
- Installing PyQt
- Creating a simple window
- Adding widgets to the window
- Running the application
Let’s get started.
- Installing PyQt:
Before we can start building our GUI, we need to install PyQt. PyQt can be installed using pip, the Python package manager. Open your terminal or command prompt and run the following command:
pip install PyQt5
This will install PyQt5, the latest version of PyQt available. Once the installation is complete, we can move on to creating our GUI.
- Creating a simple window:
To create a simple window in PyQt, we need to import the necessary modules and create an instance of the QApplication and QWidget classes.
import sys
from PyQt5.QtWidgets import QApplication, QWidget
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Simple GUI')
window.show()
sys.exit(app.exec_())
In the code snippet above, we first import the sys module and the QApplication and QWidget classes from PyQt5.QtWidgets. We then create an instance of the QApplication class with sys.argv as the argument. This is necessary for creating an application event loop. Next, we create an instance of the QWidget class and set the window title using the setWindowTitle() method. Finally, we call the show() method to display the window and sys.exit(app.exec_()) to start the application event loop.
- Adding widgets to the window:
Now that we have a simple window, let’s add some widgets to it. We can add widgets such as labels, buttons, and text boxes using PyQt.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Simple GUI')
label = QLabel('Hello, PyQt!', window)
label.move(100, 50)
button = QPushButton('Click me!', window)
button.move(100, 100)
window.show()
sys.exit(app.exec_())
In the code snippet above, we have added a QLabel widget with the text ‘Hello, PyQt!’ and a QPushButton widget with the text ‘Click me!’ to the window. We have also used the move() method to position the widgets on the window.
- Running the application:
To run the application, save the code snippet in a Python file, for example, main.py, and run it using the following command:
python main.py
You should see a simple window with a label and a button displayed on your screen. You can click the button and interact with the GUI.
That’s it for Part 1 of this PyQt tutorial for beginners. In the next part, we will cover more advanced topics such as layouts, signals, and slots. Stay tuned!
Easiest PyQt GUI Development Series you have ever seen!!
can we pack PyQt into 1 exe executable file?