In this Python PySide6 lesson we want to learn How to Create First GUI with Python & PySide6, first of all let’s talk about PySide6.
What is PySide6 ?
PySide6 (Qt for Python) is Python binding for the cross-platform graphical user interface (GUI) toolkit Qt. It is a powerful tool that can be used to develop desktop applications that are compatible with multiple operating systems such as Windows, Linux, and macOS.
Getting Started with PySide6
To get started with PySide6, you will need to install it on your system. You can do this by running the following command in your terminal:
1 |
pip install PySide6 |
Once you have installed PySide6, you can begin creating your first window.
Create First GUI with Python & PySide6
for creating basic window in PySide6, you will need to create QApplication object and QMainWindow object. The QApplication object is responsible for managing application’s event loop, while the QMainWindow object is the main window of the application.
This is a simple code to create your first GUI Window with PySide6:
1 2 3 4 5 6 7 8 9 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow app = QApplication(sys.argv) window = QMainWindow() window.show() sys.exit(app.exec()) |
In the above code, we first imported the necessary modules from PySide6. after that we create QApplication object and QMainWindow object. QMainWindow object is assigned to the window variable.
Finally, we call the show() method to display window and the sys.exit() method to start the application’s event loop.
Run the code and this will be the result.
Customizing Pyside6 Window
Now that we have created basic window in PySide6, we can customize it to suit our needs. we can add widgets such as buttons, labels, and text boxes, and also we can modify the window’s appearance by changing its size, title, and background color.
This is the code that show you how to change the window’s title and size:
1 2 3 4 5 6 7 8 9 10 11 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow app = QApplication(sys.argv) window = QMainWindow() window.setWindowTitle("Codeloop.org - PySide6 Course") window.setGeometry(100, 100, 500, 500) window.show() sys.exit(app.exec()) |
In the above code, we have added two new lines. The first line sets window’s title to “GeeksCoders.com – PySide6 Course,” and the second line sets window’s geometry to (100, 100, 500, 500), which specifies the window’s position on the screen (100 pixels from the left and 100 pixels from the top) and its size (500 pixels wide and 500 pixels high).
Run the complete code and this will be the result.