DEV Community

Alex Arroyo
Alex Arroyo

Posted on

Building an Installable Desktop App with Python, PyQt, and Py2app

Desktop applications remain a popular choice for many utilities and tools. With Python being one of the most versatile and widely used programming languages, combining it with a GUI library like PyQt can yield robust applications. In this tutorial, we’ll go through the process of creating a simple installable desktop app using Python, PyQt, and Py2app.

Prerequisites:

1. Installing Python and pip

Ensure you have both Python and pip installed. If not, here’s how you can do it:

  • Download and install Python from the official Python website.
  • Pip is included by default with Python 3.4 and later.

2. Installing necessary libraries

With pip in place, install the necessary libraries:

pip install py2app PyQt5 pyinstaller
Enter fullscreen mode Exit fullscreen mode

Creating Your Desktop Application:

1. Writing app.py

Here’s a simple PyQt application that displays a window with a greeting message:

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow

class MyApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Hello PyQt')
        self.setGeometry(100, 100, 200, 80)
        label = QLabel('Hello, world!', self)
        label.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(label)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())
Enter fullscreen mode Exit fullscreen mode

2. Creating setup.py

This script informs py2app how to package your application. Here’s a basic example:

from setuptools import setup

APP = ['app.py']
DATA_FILES = []
OPTIONS = {
    'argv_emulation': True,
    'packages': ['PyQt5'],
}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)
Enter fullscreen mode Exit fullscreen mode

Making Your Python GUI App Executable:

Execute the following command:

python setup.py py2app
Enter fullscreen mode Exit fullscreen mode

This will bundle your PyQt application into a standalone executable. Once finished, you’ll find a .app bundle in the dist directory.

Making Your Desktop App Installable:

1. Install Node.js and npm

You can download and install both from the official Node.js website.

2. Install create-dmg using npm

npm install -g create-dmg
Enter fullscreen mode Exit fullscreen mode

3. Create an installable dmg for macOS

create-dmg 'dist/YourApp.app' destination_path/
Enter fullscreen mode Exit fullscreen mode

Replace YourApp.app with your actual application name and destination_path/ with where you want the .dmg file to be saved.


And voila! You now have an installable desktop app created with Python and PyQt. This process provides a straightforward method to develop and distribute GUI applications for macOS. Happy coding!

Top comments (0)