有些时候你可能为了解决某个问题用
python
写了一个小工具,工具写好了,但是并不局限于你一个人使用,怎么才能让别人更好的使用呢,搞成二进制文件给别人?但是二进制文件传输的时候权限默认会丢,你让一个不太熟悉命令行工具的人去给文件授权?
•mac os 10.13.2•python 2.7•Tkinter•pyinstaller 3.4 工具简单使用•hdiutil 工具简单使用
这里我们使用Tkinter
工具来实现一个简单的计算器来给大家演示,代码[1]如下,源代码本身是基于python3
的,我给修改了下,在python2
上正常运行,为了兼容跨机器字体显示正常(否则,有可能打好了dmg文件,别人打开字体显示全是空白),引入了ttk
,上代码:
from Tkinter import *
import ttk
# Let's create the Tkinter window
window = Tk()
# Then, you will define the size of the window in width(312) and height(324) using the 'geometry' method
window.geometry("375x200")
# In order to prevent the window from getting resized you will call 'resizable' method on the window
window.resizable(0, 0)
#Finally, define the title of the window
window.title("Calcualtor")
# Let's now define the required functions for the Calculator to function properly.
# 1. First is the button click 'btn_click' function which will continuously update the input field whenever a number is entered or any button is pressed it will act as a button click update.
def btn_click(item):
global expression
expression = expression + str(item)
input_text.set(expression)
# 2. Second is the button clear 'btn_clear' function clears the input field or previous calculations using the button "C"
def btn_clear():
global expression
expression = ""
input_text.set("")
# 3. Third and the final function is button equal ("=") 'btn_equal' function which will calculate the expression present in input field. For example: User clicks button 2, + and 3 then clicks "=" will result in an output 5.
def btn_equal():
global expression
result = str(eval(expression)) # 'eval' function is used for evaluating the string expressions directly
# you can also implement your own function to evalute the expression istead of 'eval' function
input_text.set(result)
expression = ""
expression = ""
# In order to get the instance of the input field 'StringVar()' is used
input_text = StringVar()
# Once all the functions are defined then comes the main section where you will start defining the structure of the calculator inside the GUI.
# The first thing is to create a frame for the input field
input_frame = Frame(window, width = 312, height = 50, bd = 0, highlightbackground = "black", highlightcolor = "black", highlightthickness = 1)
input_frame.pack(side = TOP)
# Then you will create an input field inside the 'Frame' that was created in the previous step. Here the digits or the output will be displayed as 'right' aligned
input_field = Entry(input_frame, font = ('arial', 18, 'bold'), textvariable = input_text, width = 50, bg = "#eee", bd = 0, justify = RIGHT)
input_field.grid(row = 0, column = 0)
input_field.pack(ipady = 10) # 'ipady' is an internal padding to increase the height of input field
# Once you have the input field defined then you need a separate frame which will incorporate all the buttons inside it below the 'input field'
btns_frame = Frame(window, width = 312, height = 272.5, bg = "grey")
btns_frame.pack()
# The first row will comprise of the buttons 'Clear (C)' and 'Divide (/)'
clear = ttk.Button(btns_frame, text = "C",command = lambda: btn_clear()).grid(row = 0, column = 0, columnspan = 3, padx = 1, pady = 1)
divide = ttk.Button(btns_frame, text = "/", command = lambda: btn_click("/")).grid(row = 0, column = 3, padx = 1, pady = 1)
# The second row will comprise of the buttons '7', '8', '9' and 'Multiply (*)'
seven = ttk.Button(btns_frame, text = "7", command = lambda: btn_click(7)).grid(row = 1, column = 0, padx = 1, pady = 1)
eight = ttk.Button(btns_frame, text = "8", command = lambda: btn_click(8)).grid(row = 1, column = 1, padx = 1, pady = 1)
nine = ttk.Button(btns_frame, text = "9", command = lambda: btn_click(9)).grid(row = 1, column = 2, padx = 1, pady = 1)
# seven = ttk.Button(btns_frame, text = "7",command = lambda: btn_click(7)).grid(row = 1, column = 0, padx = 1, pady = 1)
# eight = ttk.Button(btns_frame, text = "8",command = lambda: btn_click(8)).grid(row = 1, column = 1, padx = 1, pady = 1)
# nine = ttk.Button(btns_frame, text = "9",command = lambda: btn_click(9)).grid(row = 1, column = 2, padx = 1, pady = 1)
multiply = ttk.Button(btns_frame, text = "*", command = lambda: btn_click("*")).grid(row = 1, column = 3, padx = 1, pady = 1)
# The third row will comprise of the buttons '4', '5', '6' and 'Subtract (-)'
four = ttk.Button(btns_frame, text = "4",command = lambda: btn_click(4)).grid(row = 2, column = 0, padx = 1, pady = 1)
five = ttk.Button(btns_frame, text = "5",command = lambda: btn_click(5)).grid(row = 2, column = 1, padx = 1, pady = 1)
six = ttk.Button(btns_frame, text = "6",command = lambda: btn_click(6)).grid(row = 2, column = 2, padx = 1, pady = 1)
minus = ttk.Button(btns_frame, text = "-", command = lambda: btn_click("-")).grid(row = 2, column = 3, padx = 1, pady = 1)
# The fourth row will comprise of the buttons '1', '2', '3' and 'Addition (+)'
one = ttk.Button(btns_frame, text = "1",command = lambda: btn_click(1)).grid(row = 3, column = 0, padx = 1, pady = 1)
two = ttk.Button(btns_frame, text = "2",command = lambda: btn_click(2)).grid(row = 3, column = 1, padx = 1, pady = 1)
three = ttk.Button(btns_frame, text = "3",command = lambda: btn_click(3)).grid(row = 3, column = 2, padx = 1, pady = 1)
plus = ttk.Button(btns_frame, text = "+", command = lambda: btn_click("+")).grid(row = 3, column = 3, padx = 1, pady = 1)
# Finally, the fifth row will comprise of the buttons '0', 'Decimal (.)', and 'Equal To (=)'
zero = ttk.Button(btns_frame, text = "0",command = lambda: btn_click(0)).grid(row = 4, column = 0, columnspan = 2, padx = 1, pady = 1)
point = ttk.Button(btns_frame, text = ".", command = lambda: btn_click(".")).grid(row = 4, column = 2, padx = 1, pady = 1)
equals = ttk.Button(btns_frame, text = "=", command = lambda: btn_equal()).grid(row = 4, column = 3, padx = 1, pady = 1)
window.mainloop()
这里是打成非单文件的形式
pyinstaller -y --clean --windowed main.py -n Calcualtor_for_zhuima
# 执行之后的结果
binanry2brew: tree -L 2
.
├── Calcualtor_for_zhuima.spec
├── build
│ └── Calcualtor_for_zhuima
├── dist
│ ├── Calcualtor_for_zhuima
│ └── Calcualtor_for_zhuima.app
└── main.py
5 directories, 2 files
# 进入dist目录下
binanry2brew: cd dist/
dist: hdiutil create ./Calcualtor_for_zhuima.dmg -srcfolder Calcualtor_for_zhuima.app -ov
# 执行之后的结果,会多出来一个dmg文件
dist: tree -L 1
.
├── Calcualtor_for_zhuima
├── Calcualtor_for_zhuima.app
└── Calcualtor_for_zhuima.dmg
2 directories, 1 file
dist:
怎么样,是不是很简单,哈哈,需要说明的一点是这个招数在一些简单粗暴的场景下可以用起来,如果是大面积推广使用,还是老老实实的编写dmg
配套的文件来实现~
[1]
代码: https://www.datacamp.com/community/tutorials/gui-tkinter-python