In this article we want to talk about PyQt5 Examples and Learn Python GUI Programming, so when it comes to Python GUI Programming, you have different options for GUI Programming and one of them is PyQt5, first of all let’s talk about PyQt5, PyQt5 is cross platform GUI toolkit for Python that allows developers to create desktop applications with easy and simple. PyQt5 provides different tools and widgets for building graphical user interfaces and this makes it popular choice for Python developers. in this article we are going to talk about some PyQt5 examples that will help you learn Python GUI programming.
Now let’s start for creating basic window in Python PyQt5, so the first step in creating any PyQt5 application is to create basic window. this is an example of how to create simple window with title using PyQt5:
1 2 3 4 5 6 7 8 |
import sys from PyQt5.QtWidgets import QApplication, QWidget app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('GeeksCoders.com') window.show() sys.exit(app.exec_()) |
In this example we have imported required modules after that we have created an instance of QApplication and created QWidget, set its title and show the window.
Run the complete code and this will be the result
In the previous example we have created basic window in Python PyQt5, now let’s add some more functionality, this time we want to add label in our window, so labels are used to display text or images on the screen. this is an example of how to add a label to our window:
1 2 3 4 5 6 7 8 9 10 11 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('GeeksCoders') label = QLabel(window) label.setText('Welcome to GeeksCoders.com!') label.move(50, 50) window.show() sys.exit(app.exec_()) |
In this example we have created QLabel, set its text and move it to position (50,50) on the screen.
Run the complete code and this will be the result
Now it is time to add buttons to our window and for adding button we can use QPushButton, buttons are used to perform actions when clicked. this is an example of how to create button using PyQt5:
1 2 3 4 5 6 7 8 9 10 11 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('GeeksCoders') button = QPushButton('Click Me', window) button.setToolTip('Welcome to GeeksCoders') button.move(50, 50) window.show() sys.exit(app.exec_()) |
In this example we have create QPushButton, set its text, add tooltip and move it to position (50,50) on the screen.
Run the complete code and this will be the result
Now let’s add menus to our Python PyQt5 GUI Programming application, menus are used to provide list of options to the user. this is an example of how to add menu using Python PyQt5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QAction class MyWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Geekscoders.com') self.setGeometry(100, 100, 300, 200) menu = self.menuBar() file_menu = menu.addMenu('File') new_action = QAction('New', self) file_menu.addAction(new_action) app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_()) |
In this example we have created QMainWindow, added menu bar, added File menu and added New action to the File menu.
Run the complete code and this will be the result
In result we can say that PyQt5 is powerful GUI toolkit that allows Python developers to create desktop applications with easy and simple. in this article we have explored some PyQt5 examples that will help you learn Python GUI programming. by building on these examples, you can create complex and feature rich desktop applications using PyQt5.
Learn More on Python
- PyQt5 QTableWidget Tutorial: Create a Dynamic Table
- Create GUI Applications with Python & TKinter
- Python TKinter Layout Management
- How to Create Label in TKinter
- How to Create Buttin in Python TKinter
- Build Music Player in Python TKinter
- Python GUI Programming with TKinter
- TKinter VS PyQt, Which one is Good
- Creating Custom Widgets in TKinter