1.基础窗口测试
创建 test.py 文件
import tkinter as tk
window = tk.Tk()#创建一个窗口
window.title('测试窗口')
window.geometry('300x100')#窗口大小
text = tk.Text(window)
text.insert(tk.INSERT,'hello')
text.pack()
tk.mainloop()
运行 pyinstaller -D -w test.py
将会生成exe 文件
2.运行测试
print ('Python script to exe test program')
count = 0
while count < 10:
print ("count = " + str(count) +"
")
count = count + 1
3.编写 计算机 并生成EXE程序
Caculater.py
1 #filename:Caculater.py 2 3 import tkinter,time,decimal,math,string 4 5 root=tkinter.Tk() #创建窗口 6 root.title('计算器') #窗口标题 7 root.resizable(0,0) #窗口大小 (框体大小可调性,分别表示x,y方向的可变性;) 8 global cuncu, vartext, result, fuhao #全全局变量 9 result = fuhao = None 10 vartext = tkinter.StringVar() 11 cuncu = [] #存入 12 13 class anjianzhi: 14 global cuncu, vartext, result, fuhao 15 def __init__(self,anjian): 16 self.anjian = anjian #按键 17 def jia(self): #加 18 cuncu.append(self.anjian) 19 vartext.set( ''.join(cuncu)) 20 def tui(self): 21 cuncu.pop() 22 vartext.set(''.join(cuncu)) 23 def clear(self): 24 cuncu.clear() 25 vartext.set('') 26 result = None 27 fuhao = None 28 def zhengfu(self): 29 if cuncu[0]: 30 if cuncu[0] == '-': 31 cuncu[0] = '+' 32 elif cuncu[0] == '+': 33 cuncu[0] = '-' 34 else: 35 cuncu.insert(0, '-') 36 vartext.set(''.join(cuncu)) 37 def xiaoshudian(self): 38 if cuncu.count('.') >= 1: 39 pass 40 else: 41 if cuncu == [] : 42 cuncu.append('0') 43 cuncu.append('.') 44 vartext.set(''.join(cuncu)) 45 def yunshuan(self): 46 global cuncu, vartext, result, fuhao 47 if vartext.get() == '': 48 pass 49 else: 50 get1 = decimal.Decimal(vartext.get()) 51 if self.anjian in ('1/x','sqrt'): 52 if self.anjian == '1/x': 53 result = 1/get1 54 elif self.anjian == 'sqrt': 55 result = math.sqrt(get1) 56 elif self.anjian in ('+','-','*','/','='): 57 if fuhao is not None: 58 get1 = decimal.Decimal(result) 59 get2 = decimal.Decimal(vartext.get()) 60 if fuhao == '+': 61 result = get1 + get2 62 elif fuhao == '-': 63 result = get1 - get2 64 elif fuhao == '*': 65 result = get1 * get2 66 elif fuhao == '/': 67 result = get1 / get2 68 else: 69 result = get1 70 if self.anjian == '=': 71 fuhao = None 72 else: 73 fuhao = self.anjian 74 print(fuhao) 75 print(result) 76 vartext.set(str(result)) 77 cuncu.clear() 78 79 def copy1(): 80 # tkinter.Misc().clipboard_clear() 81 tkinter.Misc().clipboard_append(string(vartext.get())) 82 83 def buju(root): 84 global cuncu, vartext, result, fuhao 85 #显示界面 86 entry1 = tkinter.Label(root, width=30, height=5, bg='white', anchor='se', textvariable=vartext) 87 entry1.grid(row=0, columnspan=5) 88 #第一行按钮 89 buttonMC=tkinter.Button(root,text='MC',width=5) 90 buttonMR=tkinter.Button(root,text='MR',width=5) 91 buttonMS=tkinter.Button(root,text='MS',width=5) 92 buttonM1=tkinter.Button(root,text='M+',width=5) 93 buttonM2=tkinter.Button(root,text='M-',width=5) 94 buttonMC.grid(row=1,column=0) 95 buttonMR.grid(row=1,column=1) 96 buttonMS.grid(row=1,column=2) 97 buttonM1.grid(row=1,column=3) 98 buttonM2.grid(row=1,column=4) 99 100 #第二行按钮 101 buttonJ=tkinter.Button(root,text='←',width=5,command=anjianzhi('c').tui) 102 buttonCE=tkinter.Button(root,text='CE',width=5) 103 buttonC=tkinter.Button(root,text=' C ',width=5,command=anjianzhi('c').clear) 104 button12=tkinter.Button(root,text='±',width=5,command=anjianzhi('c').zhengfu) 105 buttonD=tkinter.Button(root,text='√',width=5,command=anjianzhi('sqrt').yunshuan) 106 buttonJ.grid(row=2,column=0) 107 buttonCE.grid(row=2,column=1) 108 buttonC.grid(row=2,column=2) 109 button12.grid(row=2,column=3) 110 buttonD.grid(row=2,column=4) 111 #第三行按钮 112 button7=tkinter.Button(root,text=' 7 ',width=5,command=anjianzhi('7').jia) 113 button8=tkinter.Button(root,text=' 8 ',width=5,command=anjianzhi('8').jia) 114 button9=tkinter.Button(root,text=' 9 ',width=5,command=anjianzhi('9').jia) 115 buttonc=tkinter.Button(root, text=' / ',width=5,command=anjianzhi('/').yunshuan) 116 buttonf= tkinter.Button(root, text=' % ',width=5) 117 button7.grid(row=3,column=0) 118 button8.grid(row=3,column=1) 119 button9.grid(row=3,column=2) 120 buttonc.grid(row=3,column=3) 121 buttonf.grid(row=3,column=4) 122 #第四行按钮 123 button4=tkinter.Button(root,text=' 4 ',width=5,command=anjianzhi('4').jia) 124 button5=tkinter.Button(root,text=' 5 ',width=5,command=anjianzhi('5').jia) 125 button6=tkinter.Button(root,text=' 6 ',width=5,command=anjianzhi('6').jia) 126 buttonx=tkinter.Button(root,text=' * ',width=5,command=anjianzhi('*').yunshuan) 127 buttonfs=tkinter.Button(root,text='1/x',width=5,command=anjianzhi('1/x').yunshuan) 128 button4.grid(row=4,column=0) 129 button5.grid(row=4,column=1) 130 button6.grid(row=4,column=2) 131 buttonx.grid(row=4,column=3) 132 buttonfs.grid(row=4,column=4) 133 #第五行按钮 134 button1 = tkinter.Button(root, text=' 1 ',width=5,command=anjianzhi('1').jia) 135 button2 = tkinter.Button(root, text=' 2 ',width=5,command=anjianzhi('2').jia) 136 button3 = tkinter.Button(root, text=' 3 ',width=5,command=anjianzhi('3').jia) 137 button_= tkinter.Button(root, text=' - ',width=5,command=anjianzhi('-').yunshuan) 138 buttondy= tkinter.Button(root, text=' = ',width=5,command=anjianzhi('=').yunshuan) 139 button1.grid(row=5, column=0) 140 button2.grid(row=5, column=1) 141 button3.grid(row=5, column=2) 142 button_.grid(row=5, column=3) 143 buttondy.grid(row=5, column=4,rowspan=2) 144 #第六行按钮 145 button0=tkinter.Button(root,text=' 0 ',width=11,command=anjianzhi('0').jia) 146 buttonjh = tkinter.Button(root,text=' . ',width=5,command=anjianzhi('c').xiaoshudian) 147 buttonjia=tkinter.Button(root,text=' + ',width=5,command=anjianzhi('+').yunshuan) 148 button0.grid(row=6,column=0,columnspan=2) 149 buttonjh.grid(row=6,column=2) 150 buttonjia.grid(row=6,column=3) 151 def caidan(root): #菜单 152 153 menu=tkinter.Menu(root) 154 submenu1=tkinter.Menu(menu,tearoff=0) 155 menu.add_cascade(label='查看',menu=submenu1) 156 submenu2 = tkinter.Menu(menu, tearoff=0) 157 submenu2.add_command(label='复制') 158 submenu2.add_command(label='粘贴') 159 menu.add_cascade(label='编辑',menu=submenu2) 160 submenu = tkinter.Menu(menu, tearoff=0) 161 submenu.add_command(label='查看帮助') 162 submenu.add_separator() 163 submenu.add_command(label='关于计算机') 164 menu.add_cascade(label='帮助',menu=submenu) 165 root.config(menu=menu) 166 167 168 buju(root) 169 caidan(root) 170 root.mainloop()
将文件Caculater.py 放入D盘:D://Caculater.py
Anacoda promt 中目录切换到D盘,输入pyinstaller -D -w Caculater.py 将生成dist/test 文件目录,运行里面的exe文件,如图
-
-F, --onefile Py代码只有一个文件
-
-D, --onedir Py代码放在一个目录中(默认是这个)
-
-K, --tk 包含TCL/TK
-
-d, --debug 生成debug模式的exe文件
-
-w, --windowed, --noconsole 窗体exe文件(Windows Only)
-
-c, --nowindowed, --console 控制台exe文件(Windows Only)
-
-X, --upx 使用upx压缩exe文件
-
-o DIR, --out=DIR 设置spec文件输出的目录,默认在PyInstaller同目录
-
-i con=FILE.ICO 加入图标(Windows Only)
-
-v FILE, --version=FILE 加入版本信息文件