Create a native GUI app with Py2app and Tkinter

Introduction

I thought I had never made a GUI app with Python, so I tried it. Also, it is not interesting to write only the GUI script, so I tried to output and execute the script in the Mac native application format.

What to use

code

I brought the code from tkinter's official documentation almost as it is.

The changed part was that the say_hi method outputs a message with a print statement, so I tried to display a message box there.

On the way, even though I imported all Tkinter modules, I was told that the tkMessageBox class could not be found and I had a little trouble. (I didn't think the tkMessageBox class was a top-level class ...)

tk.py


from Tkinter import *
import tkMessageBox

class Application(Frame):
    def say_hi(self):
        tkMessageBox.showinfo('info', 'hi there, everyone!')

    def createWidgets(self):
        self.QUIT = Button(self)
        self.QUIT["text"] = "QUIT"
        self.QUIT["fg"]   = "red"
        self.QUIT["command"] =  self.quit

        self.QUIT.pack({"side": "left"})

        self.hi_there = Button(self)
        self.hi_there["text"] = "Hello",
        self.hi_there["command"] = self.say_hi

        self.hi_there.pack({"side": "left"})

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

When executed, it looks like this.

main window

Click Hello.

info message

Yes, the message box came out safely. As a result, I'm a little sad, but I'll proceed as it is.

Wrap with Py2app

First, let's put Py2app with pip.

$ pip install py2app

If you follow the tutorial, it seems that setup.py is required first. There seems to be a convenient command called py2applet, so use it to create setup.py.

$ py2applet --make-setup tk.py

Then run setup.py to create an app.

$ python setup.py py2app

** 2017/02/09 Fixed ** Removed from the above command because it cannot be executed from other environments with the -A parameter.

When completed, the files will be placed under the dist directory as shown below.

dist

Double-click on the file and it ran successfully! It's pretty easy. I found that writing a GUI app in Python is a fairly low hurdle, so I'd like to try various things.

reference

Recommended Posts

Create a native GUI app with Py2app and Tkinter
Create a GUI app with Python's Tkinter
Let's make a Mac app with Tkinter and py2app
Create a GUI executable file created with tkinter
Create a Mac app using py2app and Python3! !!
Create a python GUI using tkinter
Easy GUI app with Tkinter Text
Create a simple GUI app in Python
Create a simple web app with flask
Create a Todo app with Django ④ Implement folder and task creation functions
Create a frame with transparent background with tkinter [Python]
You can easily create a GUI with Python
Create a matrix with PythonGUI (tkinter combo box)
How to create a multi-platform app with kivy
Try to dynamically create a Checkbutton with Python's Tkinter
Create a Todo app with the Django REST framework
Create a Todo app with Django ③ Create a task list page
Create a batch of images and inflate with ImageDataGenerator
Create a 3D model viewer with PyQt5 and PyQtGraph
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a Todo app with Django ⑤ Create a task editing function
[Linux] Create a self-signed certificate with Docker and apache
Programming with Python and Tkinter
Working with tkinter and mouse
Create a homepage with django
Create Image Viewer with Tkinter
Create a heatmap with pyqtgraph
Create a directory with python
[Python] Create a file & folder path specification screen with tkinter
Create a web surveillance camera with Raspberry Pi and OpenCV
Create a machine learning app with ABEJA Platform + LINE Bot
Create a life game that is manually updated with tkinter
Create applications, register data, and share with a single email
Let's create a PRML diagram with Python, Numpy and matplotlib.
[Learning record] Create a mysterious dungeon game with Pyhton's Tkinter
Creating a GUI as easily as possible with python [tkinter edition]
Create a deploy script with fabric and cuisine and reuse it
Create a Todo app with Django ① Build an environment with Docker
I tried to make GUI tic-tac-toe with Python and Tkinter
Steps to set up Pipenv, create a CRUD app with Flask, and containerize it with Docker
Let's create a tic-tac-toe AI with Pylearn 2-Save and load models-
Let's make a GUI with python.
Create a temporary file with django as a zip file and return it
Create a striped illusion with gamma correction for Python3 and openCV3
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Create file update history and brief release notes with PySImple GUI
Create a virtual environment with Python!
Create and deploy a Django (PTVS) app using Azure Table storage
Creating a simple app with flask
[GUI with Python] PyQt5-Drag and drop-
Async / await with Kivy and tkinter
Create a private DMP with zero initial cost and zero development with BigQuery
Make a scraping app with Python + Django + AWS and change jobs
I tried to create Bulls and Cows with a shell program
Create a poisson stepper with numpy.random
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Draw a graph with PySimple GUI
Create a simple Python development environment with VS Code and Docker
Create a file uploader with Django
Create and return a CP932 CSV file for Excel with Chalice
[AWS] Create a Python Lambda environment with CodeStar and do Hello World