This time there is a python program created in previous article, but due to wind rumors, it is easy to make a Python application using py2app, so I am eager to actually make it an application. is. Please enjoy until the end m (_ _) m ✳︎ This article assumes that Python3, Tkinter, and pip3 are installed.
You might be wondering why you need to make an app even though it's easy to run a program. There are two reasons I think. Part 1: Making it into an app makes it easier to share. Part 2: Making it easier to distribute.
I can't talk about it without a program to make it an app. So let's prepare Python code using Tkinter. Please use this if you like. However, there was a person who posted a cleaner code than me in the comment of Last article, so I think it would be good to refer to the one written by that person.
import tkinter as tk
import random
#Processing when the button is pressed
def dice(event):
#Generate a random integer and rewrite the contents of label
value["text"] = random.randint(1,6)
root = tk.Tk()
root.title("Dice.app")
root.geometry('200x150')
value = tk.Label(text="0",font=("",80))
value.pack(fill = 'x', padx=20, side = 'top')
Btn = tk.Button(text='Roll the dice',width=10)
Btn.bind("<Button-1>", dice)
Btn.pack(fill = 'x', padx=20, side = 'top')
root.mainloop()
Create a folder called app and put the program file in app earlier. Then issue terminal and type the following command
pip3 install py2app
This completes the installation of py2app. Then go to the app folder in the terminal and type the following command (please replace the dice with the name of your saved file)
py2applet --make-setup dice.py
This command will generate setup.py.
Next, prepare the icns image. This image will be the app icon. First, prepare a suitable image. I used the photo of this site. Next, download the app called Image2icon. Create an icns image with this app. Once installed, launch the app and drag and drop the image you want to make into an icon. After that, select ICNS from the export button, specify the save location in app, and wait for ICNS to be created.
Open setup.py and change it as follows (change the name of your program in APP and the name of your icns file in dice.icns in OPTIONS)
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['dice.py']
DATA_FILES = []
OPTIONS = {'iconfile': 'dice.icns'}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Finally, hit this command on the terminal to finish.
python3 setup.py py2app
The app is now complete. The actual app is stored in the dist of the app folder!
I was only able to develop apps with Xcode, so I was surprised to be able to create apps in such an instant. Everyone, please try to make an app! !!
✳︎ This article is written by a beginner in the program, so feel free to comment.
Reference site: https://ebi-works.com/python-desktop-app/
Recommended Posts