22
33import os .path
44from tkinter import (Button , Checkbutton , Entry , IntVar , Label , LabelFrame , StringVar , filedialog )
5-
5+ from os import system
6+ from threading import Thread
7+ from utils import set_window_center
68
79class View (object ):
810 def __init__ (self , master = None ):
@@ -12,13 +14,20 @@ def __init__(self, master=None):
1214
1315 def init_view (self ):
1416 '''基本框架'''
15- self .frm_project = LabelFrame (self .root , text = '项目' )
16- self .frm_config = LabelFrame (self .root , text = '配置' )
17- self .frm_operate = LabelFrame (self .root , text = '操作' )
18- self .frm_status = LabelFrame (self .root , text = '状态' )
17+ self .frm_main = LabelFrame (self .root , borderwidth = 0 )
18+ self .frm_main .pack ( side = 'left' , fill = 'y' )
19+
20+ self .frm_advance = LabelFrame (self .root , text = '高级选项' )
21+ # self.frm_advance.pack(expand='yes', side='right', fill='both', padx=15, pady=10)
22+ # self.frm_2 = LabelFrame(self.frm_advance, text='高级配置', width=300)
23+ # self.frm_2.pack(expand='yes', side='top', fill='both', padx=15, pady=10)
24+
25+ self .frm_project = LabelFrame (self .frm_main , text = '项目信息' )
26+ self .frm_config = LabelFrame (self .frm_main , text = '配置信息' )
27+ self .frm_operate = LabelFrame (self .frm_main , text = '操作' )
28+ self .frm_status = LabelFrame (self .frm_main , text = '状态' )
1929
20- self .frm_project .pack (expand = 'yes' , side = 'top' ,
21- fill = 'both' , padx = 15 , pady = 10 )
30+ self .frm_project .pack (expand = 'yes' , side = 'top' , fill = 'both' , padx = 15 , pady = 10 )
2231 self .frm_config .pack (fill = 'x' , padx = 15 , pady = 10 )
2332 self .frm_operate .pack (fill = 'x' , padx = 15 , pady = 10 )
2433 self .frm_status .pack (side = 'bottom' , fill = 'x' , padx = 15 , pady = 10 )
@@ -68,8 +77,11 @@ def init_config(self):
6877 self .cfg_onedir = IntVar (value = 0 )
6978 self .cfg_noconsole = IntVar (value = 1 )
7079 self .cfg_clean = IntVar (value = 1 )
80+ self .cfg_upx = IntVar (value = 1 ) # UPX 默认开启
7181 self .cfg_rename = IntVar ()
7282 self .cfg_exe_name = StringVar ()
83+ # 自定义配置文件
84+ self .cfg_specfile = StringVar (value = 'build.spec' )
7385 # 子配置框架
7486 self .frm_config_base = LabelFrame (
7587 self .frm_config , text = '基本' , borderwidth = 0 )
@@ -80,11 +92,16 @@ def init_config(self):
8092 self .frm_config_other = LabelFrame (
8193 self .frm_config , text = '其它' , borderwidth = 0 )
8294 self .frm_config_other .pack (fill = 'x' , padx = 10 , pady = 5 , ipady = 5 )
95+ self .frm_config_spec = LabelFrame (self .frm_config , text = '配置文件' , borderwidth = 0 )
96+ self .frm_config_spec .pack (fill = 'x' , padx = 10 , pady = 5 , ipady = 5 )
97+
8398 # 定义按钮
8499 self .btn_noconsole = Checkbutton (
85100 self .frm_config_base , text = '关闭控制台' , variable = self .cfg_noconsole )
86101 self .btn_clean = Checkbutton (
87102 self .frm_config_base , text = '构建前清理' , variable = self .cfg_clean )
103+ self .btn_upx = Checkbutton (
104+ self .frm_config_base , text = 'UPX压缩' , variable = self .cfg_upx )
88105 self .btn_isonefile = Checkbutton (
89106 self .frm_config_exe , text = '独立执行文件' , variable = self .cfg_onefile )
90107 self .btn_isonedir = Checkbutton (
@@ -94,13 +111,18 @@ def init_config(self):
94111 self .entry_rename = Entry (
95112 self .frm_config_other , textvariable = self .cfg_exe_name )
96113
114+ # self.btn_rename = Checkbutton(self.frm_config_spec, text='生成配置文件', variable=self.cfg_specfile)
115+ self .entry_specfile = Entry (self .frm_config_spec , textvariable = self .cfg_specfile )
116+
97117 # 放置按钮
98118 self .btn_isonefile .pack (side = 'left' , fill = 'x' )
99119 self .btn_isonedir .pack (side = 'left' , fill = 'x' )
100120 self .btn_noconsole .pack (side = 'left' , fill = 'x' )
101121 self .btn_clean .pack (side = 'left' , fill = 'x' )
122+ self .btn_upx .pack (side = 'left' , fill = 'x' )
102123 self .btn_rename .pack (side = 'left' , fill = 'x' )
103124 self .entry_rename .pack (fill = 'x' )
125+ self .entry_specfile .pack (fill = 'x' )
104126
105127 # 变量自动切换操作
106128 self .cfg_onefile .trace ('w' , self .cfg_onefile_trace )
@@ -125,10 +147,14 @@ def init_operate(self):
125147 self .frm_operate , text = '清理' , command = self .fn_clear )
126148 self .btn_reset = Button (
127149 self .frm_operate , text = '重置' , command = self .fn_reset )
150+ self .btn_advance = Button (
151+ self .frm_operate , text = '高级选项' , command = self .fn_toggle_advance )
152+
128153 # 放置按钮
129154 self .btn_build .pack (fill = 'x' , side = 'left' )
130155 self .btn_clear .pack (fill = 'x' , side = 'left' )
131156 self .btn_reset .pack (fill = 'x' , side = 'left' )
157+ self .btn_advance .pack (fill = 'x' , side = 'right' )
132158
133159 def init_status (self ):
134160 '''状态栏'''
@@ -137,7 +163,31 @@ def init_status(self):
137163
138164 def fn_build (self ):
139165 '''生成可执行文件'''
140- pass
166+ if not self .status_build :
167+ thread_build = Thread (target = self .fn_thread )
168+ thread_build .setDaemon (True )
169+ thread_build .start ()
170+ else :
171+ self .label_status ['text' ] = '正在打包,请稍后再操作!'
172+
173+ def fn_thread (self ):
174+ '''线程执行生成动作'''
175+ if len (self .entry_value_list [0 ].get ()) == 0 :
176+ self .label_status ['text' ] = '请选择源文件'
177+ return
178+ self .status_build = True
179+ cmd = self .fn_build_cmd ()
180+ print (cmd )
181+ self .label_status ['text' ] = '正在打包,请稍等。。。'
182+ try :
183+ # PyInstaller.__main__.run(cmd)
184+ system (' ' .join (cmd ))
185+ # call(split(' '.join(cmd)), shell=True)
186+ self .status_build = False
187+ self .label_status ['text' ] = '打包成功!'
188+ except Exception as e :
189+ self .label_status ['text' ] = str (e )
190+ self .status_build = False
141191
142192 def fn_clear (self ):
143193 '''清理生成文件'''
@@ -151,9 +201,19 @@ def fn_reset(self):
151201 self .cfg_onefile .set (1 )
152202 self .cfg_noconsole .set (1 )
153203 self .cfg_clean .set (1 )
204+ self .cfg_upx .set (1 )
154205 self .cfg_rename .set (0 )
155206 self .cfg_exe_name .set ('' )
156207
208+ def fn_toggle_advance (self ):
209+ '''切换高级选项界面'''
210+ if self .frm_advance .winfo_ismapped ():
211+ set_window_center (self .root , width = (self .root .winfo_width () - 400 ))
212+ self .frm_advance .pack_forget ()
213+ else :
214+ set_window_center (self .root , width = (self .root .winfo_width () + 400 ))
215+ self .frm_advance .pack (expand = 'yes' , side = 'right' , fill = 'both' , padx = 15 , pady = 10 )
216+
157217 def fn_select_main (self ):
158218 '''选择源文件'''
159219 types = (
@@ -204,6 +264,8 @@ def fn_build_cmd(self):
204264 cmds = []
205265 cmds .append ('pyinstaller' )
206266 cmds .append ('--windowed' )
267+ cmds .append ('-y' )
268+ cmds .append ('--noconfirm' )
207269 # cmds.append('--filenames=build.spec')
208270 # cmds.append('/usr/local/bin/pyinstaller')
209271
@@ -216,6 +278,9 @@ def fn_build_cmd(self):
216278 cmds .append ('--clean' )
217279 cmds .append ('--noconfirm' )
218280
281+ if self .cfg_upx .get () == 0 :
282+ cmds .append ('--noupx' )
283+
219284 if self .cfg_noconsole .get () == 1 :
220285 cmds .append ('--noconsole' )
221286
@@ -234,8 +299,8 @@ def fn_build_cmd(self):
234299
235300 if len (self .entry_value_list [0 ].get ()) > 0 :
236301 cmds .append (self .entry_value_list [0 ].get ())
302+ # print(' '.join(cmds))
237303 return cmds
238- # return ' '.join(cmds)
239304
240305if __name__ == '__main__' :
241306 from tkinter import Tk
0 commit comments