Wen/lovexiaov (author of Jane's book)
Original link: Http://www.jianshu.com/p/afb6b2b97ce9
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
Premise
Create a normal folder and create a virtualenv environment:
# Create a custom directory$ mkdir SandwichApp$ cd SandwichApp# Use virtualenv to create an isolated environment$ virtualenv venv$ . venv/bin/activate
Now, create a simple Tkinter app and name it Sandwich.py
:
Import Sysif Sys.version_info < (3, 0): # python 2 import Tkinter as Tkelse: # python 3 import Tkinter As Tkroot = Tk. TK () root.title ("Sandwich") Tk. button (root, text= "Make Me a Sandwich"). Pack () Tk.mainloop ()
This small application is like this:
App_screen.png installation Py2app
The original Py2app due to the Modulegraph update version produced a bug. I fork the project, fix the bug, and put it on Github. To install Py2app using PIP:
install -U git+https://github.com/metachris/[email protected]master
Create
setup.py
File
Py2app includes a py2applet
tool that can help you create setup.py files:
--make-setup Sandwich.pyWrote setup.py
setup.py
A file is a basic definition of an app:
From setuptools Import Setupapp = [' sandwich.py ']data_files = []options = {' argv_emulation ': True}setup ( app=app,
data_files=data_files, options={' Py2app ': Options}, setup_requires=[' Py2app '],)
如果你的应用使用的其他文件,比如 JSON 文件,文本文件,图片等,你应该将他们包含在 DATA_FILES
中。 例如:
DATA_FILES = [‘testdata.json‘, ‘picture.png‘]
Create apps for Dev and beta
Py2app creates a setup.py
standalone application based on the definition of a file.
For method testing and development, Py2app provides an "alias pattern" that is built using symbolic links to development files.
$ python setup.py py2app -A
This command creates the following files and folders:
.├──build│└──bdist.macosx-10.10-x86_64│└──python2.7-standalone│└──app│├──frameworks│├──collect│ ├──lib-dynload│└──temp├──sandwich.py├──dist│└──sandwich.app│└──contents│├── Info.plist│├──macos││├──sandwich││└──python-/users/chris/projects/chris/py thon-gui/tkinter/env/bin/. /bin/python│├──pkginfo│└──resources│├──__boot__.py│├──__error__.sh │├──lib││└──python2.7││├──config /users/chris/projects/chris/python-gui/tkinter/env/bin/. /lib/python2.7/CONFIG││└──SITE.PYC. /.. /site.pyc│├──site.py│└──site.pyc└──setup.py
This is not a standalone application, and apps built with alias patterns do not apply to other machines.
An application built in alias mode directly refers to the source file, so any Sandwich.py
modifications to the file will take effect the next time the app starts.
dist/Sandwich.app
The development application that is located can be started in the Finder or through the Open command () as with other. App Apps. $ open dist/Sandwich.app
You can start your app at the terminal by executing the following command:
$ ./dist/Sandwich.app/Contents/MacOS/Sandwich
Build a Release app
When the test passes, you can generate the python setup.py py2app
release by calling. Make sure that build
both the old and the dist
file classes have been deleted:
$ rm -rf build dist$ python setup.py py2app
This command will package your app as a dist/Sandwich.app
. Since the app is self-contained, at any moment, if you modify the code, data files, options, etc., you can run the Py2app command again to rebuild.
The original Py2app has a bug that will appear " AttributeError: ‘ModuleGraph‘ object has no attribute ‘scan_code‘
" or load_module
. If you encounter this error, refer to StackOverflow or use my Py2app fork.
At this point, the simplest way to package and publish your app is to right-click the app in the Finder and select Create Archive.
Add an icon:
Add in the OPTIONS
dictionary "iconfile": "youricon.icns"
:
From setuptools Import Setupapp = [' sandwich.py ']data_files = []options = { ' argv_emulation ': True, ' iconfile ': ' App.icns '}setup ( App=app, data_files=data_files, options={' Py2app ': Options}, setup_requires=[ ' Py2app '],)
You can find the ICNS format icon (for example: Iconfinder or Freepik) on the Internet.
Apply Advanced Settings
You can Info.plist
invoke the behavior of the app's information by modifying it. The most complete reference to the available keys is Apple's Runtime configuratin guidelines.
Here is an example of more changes:
#-*-Coding:utf-8-*-from setuptools Import setupapp = [' sandwich.py ']app_name = ' supersandwich ' data_files = []OPTIONS = { ' argv_emulation ': True, ' iconfile ': ' App.icns ', ' plist ': { ' cfbundlename ': app_name, ' Cfbundledisplayname ': app_name, ' cfbundlegetinfostring ': "Making sandwiches", ' cfbundleidentifier ': " Com.metachris.osx.sandwich ", ' cfbundleversion ':" 0.1.0 ", ' cfbundleshortversionstring ':" 0.1.0 ", ' Nshumanreadablecopyright ': U "copyright©2015, Chris Hager, All rights Reserved" }}setup ( name=app_name, App=app, data_files=data_files, options={' Py2app ': Options}, setup_requires=[' Py2app '],)
By setting, the app will have the following information:
Get_info.png
Write a standalone Mac OS X app with Python and Py2app