Posted on 22nd October, 2022
Pyside6 is a UI framework for Python similar to Tkinter. It is based on QT Framework originally written for C++. In this concise post we would create an elementary PySide6 UI app and create an exe for it.
It is always a good practice to create a virtual environment which would house your Python packages before you begin your project. So let's start with creating a virtual environment first and activating it. I'd be developing this app in windows.
- python -m venv venv
- cd venv/Scripts
- activate.bat
Next step would be to install packages, we would only install what is required for the very primitive version of the app to run. Let's install pyinstaller and Pyside6 packages.
pip install PySide6
pip install pyinstaller
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
# Only needed for access to command line arguments
import sys
app = QApplication(sys.argv)
# Create a Qt widget, which will be our window.
window = QWidget()
layout = QVBoxLayout()
labelWindow = QLabel("Another Window")
layout.addWidget(labelWindow)
window.setLayout(layout)
window.show()
# Start the event loop.
app.exec_()
We have created a simple PySide6 app above. This is a very elementary app which only contains a label and a window. I'd explain now what each of those lines do. QApplication is responsible for creation of main application window. It calls the exec_() method which runs the desktop app.
QWidget is the canvas of the main app where we have a layout by creating an object from the QLayout class of the library. We are using a vertical layout through QVBoxLayout. After layout creation we have simply added a label. We can run this app now by running this file. Let's save this file as 'main.py'
Now try running this Python script. You should see a pop up window opened with text 'Another Window'. This is your PySide6 app, a very basic one indeed. Our focus for now is to be able to open this app as an executable on Windows. For this type the following command :-
pyinstaller --onefile --windowed main.py
Pyinstaller is the library we would use to bundle this python program along with it's dependencies into a single executable file which you would be able to open as a normal app on Windows. I'd briefly explain what those '--onefile' and '--windowed' parameters meant while converting Python file into executable. One file command means that there should only be a single file, if we skip this, you would have several DLL files along with the executable file. Windowed means we do not wish command line to be opened while we open our app. Sometimes, this might be a desirable behaviour if we plan to pass command line arguments to the app, but in this case we would avoid that.
You should now see a folder named 'Dist' in the same app directory which would have your exe. It should run the program as executable file if everything went fine. I haven't explored how this build works on Linux operating system, but might add that case in future. The above script is tested in Windows and seems to work just fine. With that I'd be wrapping up for today. Thanks for the read!
This post does not contain any images.
Have something to share ? Please post it in the comments section.
You must be logged in through your Google account to post comments
Posted by Apfirebolt on 23rd October, 2022
Nice post! I prefer Pyqt over Tkinter too