Build a Python GUI App in Apple M1 | by Andrew Zhu

Use the latest PyQT6 or PySide6 to build cross-platform desktop applications on Apple silicon chips

I’m using Python in my beloved M1 MacBook Air. Was wondering, can I use Python to build GUI desktop applications in the little power machine? After some errors and trials, I found a way, and it will work for you too.

Make sure you have M1 native running Python 3.9 installed in your M1 machines. If you don’t have it, run this one line command to prepare your environment.

brew install python@3.9

yes only a line command.

PyQt6 and PySide6 are 99.99% Same, The existence of PySide6 is due to licensing. PyQt is being used under GPL or commercial license. Which means you need to open source your code or buy a commercial license for your application which will charge users a fee. While PySide is available under the LGPL license, there is no need to purchase a license for your application.

In the following code sample, PyQt6 and PySide6 are interchangeable. I will be using PySide6 in all sample codes.

Starting from version 6.2, PyQt fully supports Apple Silicon Chip, Just run this command to install PyQt6.

pip install PyQt6

either

pip install PySide6

Complete. For more information about PyQt6 installation, see this Contact,

By the way, if you want to check what version of PySide you are using, use this code:

import PySide6.QtCore
# Prints PySide6 version
print(PySide6.__version__)
# Prints the Qt version used to compile PySide6
print(PySide6.QtCore.__version__)

create a new file helloworld.py and write the following python code.

from PySide6.QtWidgets import QApplication,QLabel
app = QApplication([])
label = QLabel('hello world')
label.show()
app.exec()

run it now python helloworld.py in your terminal. You will see a new application pop up like this:

Let’s add a few more elements to the Hello World desktop application.

In the above code, I added a Window which contains a vertical layout object, and the layout object contains all the widget elements Label And Button,

The first button (btn1) is connected to an event handler – on_btn1_clicked() So that whenever the button is clicked the event handling function is triggered.

Run it, and you’ll see.

It is unrealistic to ask your application users to install all required packages before installing the application. Therefore, you will need to compile and package all the code in a distributable form. people usually call this process Freeze,

There are many solutions for compiling PyQt applications. I found cx_Freeze Works best for Apple silicon chip.

To install cx_Freeze,

pip install --upgrade cx_Freeze

To compile your PyQt application, in your terminal, run:

cxfreeze -c helloworld.py --target-dir dist

Now, navigate to Dist folder, you can start your APP

./helloworld

all done.

  1. Qt on Apple Silicon: https://www.qt.io/blog/qt-on-apple-silicon
  2. PyQt5 Tutorial: https://build-system.fman.io/pyqt5-tutorial
  3. PyQt6 6.2.2: https://pypi.org/project/PyQt6/
  4. cx_freeze: https://cx-freeze.readthedocs.io/en/latest/index.html
  5. PyQt5 vs PySide2: https://www.pythonguis.com/faq/pyqt5-vs-pyside2/
  6. PySide6 official docs:

Leave a Reply