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.
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.
Click Hello.
Yes, the message box came out safely. As a result, I'm a little sad, but I'll proceed as it is.
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.
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.
Recommended Posts