In this PyQt tutorial we want to build our first Hello World GUI application, but before that we need to talk about PyQt, and also we need to talk about the installation process of PyQt.
What is PyQt?
PyQt is a set of Python bindings for Qt libraries, You can use PyQt for building cross-platform applications with graphical user interface or GUI. In this tutorial, we will learn how to create a simple Hello World application using PyQt5. This is a great starting point for anyone new to PyQt and GUI programming.
PyQt Installation
First of all we need to install PyQt, and we can use pip for that, open your command prompt or terminal and write this command.
1 |
pip install PyQt5 |
Creating “Hello World” Application in PyQt
Now let’s create our simple GUI application in PyQt, this is a step by step guide.
1: Import Necessary Modules
First, import the required modules from PyQt5:
1 2 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel import sys |
2: Define Main Window Class
Now we need to create a class that inherits from QMainWindow. This class will define main window of your application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class MainWindow(QMainWindow): def __init__(self): super().__init__() # Set window title self.setWindowTitle("Hello World Application") # Set window icon self.setWindowIcon(QIcon('codeloop.png')) # Set window geometry (x, y, width, height) self.setGeometry(100, 100, 280, 80) # Create label widget self.label = QLabel("Hello, World", self) self.label.move(60, 30) |
3: Initialize and Run Application
In your main section of the code, first you need to create an instance of QApplication, and after that an instance of your MainWindow class, and lastly start application event loop:
1 2 3 4 5 6 7 8 9 10 11 12 |
if __name__ == "__main__": # Create application object app = QApplication(sys.argv) # Createinstance ofmain window window = MainWindow() # Show main window window.show() # Show main window sys.exit(app.exec_()) |
This is the complete code for this article
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 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel import sys from PyQt5.QtGui import QIcon class MainWindow(QMainWindow): def __init__(self): super().__init__() # Set window title self.setWindowTitle("Hello World Application") # Set window icon self.setWindowIcon(QIcon('codeloop.png')) # Set window geometry self.setGeometry(100, 100, 280, 80) # Create label widget self.label = QLabel("Hello, World", self) self.label.move(60, 30) if __name__ == "__main__": # Create an application object app = QApplication(sys.argv) # Create an instance of main window window = MainWindow() # Show main window window.show() # Show main window sys.exit(app.exec_()) |
Now run your PyQt application and this will be the result
FAQ:
What is PyQt used for?
PyQt is Python bindings for Qt libraries, You can use PyQt for building cross platform applications. Using PyQt you can create applications that can run on different operating systems like Windows, macOS and Linux with native look and feel.
Is PyQt better than tkinter?
The choice between PyQt and tkinter depends on your specific needs and preferences:
- PyQt:
- More advanced and feature-rich, good for complex applications.
- Provides modern look and feel with more widgets and customization options.
- Has a steeper learning curve and larger documentation.
- Used in professional and commercial applications.
- tkinter:
- Simpler and easier to learn, good for small to medium-sized projects.
- Comes bundled with Python, so you don’t need for additional installation.
- Limited in terms of modern UI components and customization.
- Ideal for beginners and simple applications.
In summary we can say that for complex applications we can use PyQt, and for simpler projects or beginners, tkinter is a good choice.
Can I use PyQt for free?
Yes, you can use PyQt for free under GNU General Public License (GPL). This means that you can develop and distribute open-source applications using PyQt without any cost. But if you want to build commercial applications, then you need to purchase a commercial license from Riverbank Computing.
Subscribe and Get Free Video Courses & Articles in your Email