macos-app-py2app-tkinter

Easy introduction to macOS App using py2app

Recently, I became interested in creating dashboard with Python.

First option, it’s possible to create the custom tool using a web framework (like Flask or Django) locally, or on a website if deployment (see Gunicorn).

Then the other option is to create software. No Visual Basic, nor Java, nor C ++. I know that even famous applications have been built in Python (Instagram, Spotify, DropBox, Uber, Netflix, ..). I decide after some research to test the creation of a MacOS application.

The purpose of this article: With Python, create a MacOS application using the Tkinter GUI and using py2app.

Tkinter: Python’s de-facto standard GUI (Graphical User Interface) package.

Py2app: Python setuptools command which will allow you to make standalone Mac OS X application bundles and plugins from Python scripts.

 

NOTE: For Windows, check Py2exe

Okay, let's create our app!

For this article, it’s not about creating the best app of the year, but getting familiar with the steps in the simplest way:

  1. Create a Python program using tkinter
  2. Create the setup.py file
  3. Create the application

1. Create a Python program using tkinter

You coded your Python program with tkinter? Great you are ready for the next step. For me in this exercise, I will use a code provided by tutorialsteacher.com. You enter two numbers and you choose one of the two buttons to add or subtract.

tkinter import *
class MyWindow:
    def __init__(self, win):
        self.lbl1=Label(win, text='First number')
        self.lbl2=Label(win, text='Second number')
        self.lbl3=Label(win, text='Result')
        self.t1=Entry(bd=3)
        self.t2=Entry()
        self.t3=Entry()
        self.btn1 = Button(win, text='Add')
        self.btn2=Button(win, text='Subtract')
        self.lbl1.place(x=100, y=50)
        self.t1.place(x=200, y=50)
        self.lbl2.place(x=100, y=100)
        self.t2.place(x=200, y=100)
        self.b1=Button(win, text='Add', command=self.add)
        self.b2=Button(win, text='Subtract')
        self.b2.bind('<Button-1>', self.sub)
        self.b1.place(x=100, y=150)
        self.b2.place(x=200, y=150)
        self.lbl3.place(x=100, y=200)
        self.t3.place(x=200, y=200)
    def add(self):
        self.t3.delete(0, 'end')
        num1=int(self.t1.get())
        num2=int(self.t2.get())
        result=num1+num2
        self.t3.insert(END, str(result))
    def sub(self, event):
        self.t3.delete(0, 'end')
        num1=int(self.t1.get())
        num2=int(self.t2.get())
        result=num1-num2
        self.t3.insert(END, str(result))

window=Tk()
mywin=MyWindow(window)
window.title('Hello Python')
window.geometry("400x300+10+10")
window.mainloop()

2. Create the setup.py file

Ok, to run our application, we need to create this setup.up file. With a code editor or simply from your Terminal entering this command: py2applet --make--setup Sandwich.py

Note: My Python program for exercise is called Sandwich.py.

Also, make sure you’re in the right directory using Terminal. Ex: cd Desktop/Data\ Work/Exploration/191017\ py2app\ tkinter\ test ( = ‘\’ is needed to handle spaces).

from setuptools import setup
APP = ['Sandwich.py']
DATA_FILES = []
OPTIONS = {'iconfile':'Sandwich-app.icns',
           'argv_emulation': True,
           'plist': {'PyRuntimeLocations': [
                     '/anaconda3/lib/libpython3.7m.dylib']}}
setup(app=APP,
      data_files=DATA_FILES,
      options={'py2app': OPTIONS},
      setup_requires=['py2app'],)

OPTIONS plist: I had a bug with the application, an internal error, and had to edit PyRuntimeLocations. That’s why I added the plist option with the libpython address (got solution on stackoverflow).

OPTIONS iconfile: Enter the file name of your icon. Icns format (see png to icns).

3. Create the application

You know what ? We finished. Drop your program, icon and setup.py in the folder (directory) you want. Select the folder in your Terminal and enter the command: python setup.py py2app -A

Yeeah, your app is created. Go to your directory. You now have two new folders (build, and dist). Go to dist. And open your application. Like any application, you can save it to the dock.

That’s all for this article, it was an introduction to creating a macOS application with Python. And from here we should be able to create a beautiful dashboard, with even real-time data!

>>> Tuto tkinter 1
>>> Tuto tkinter 2
>>> Docs on standalone app
>>> Vidéo Py2app tuto
>>> Deploy Flask app
>>> Png to Icns

– – –
And you, is there a Python application that you want to share?
– – –

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *