2
2
3
3
import os .path
4
4
from 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
6
8
7
9
class View (object ):
8
10
def __init__ (self , master = None ):
@@ -12,13 +14,20 @@ def __init__(self, master=None):
12
14
13
15
def init_view (self ):
14
16
'''基本框架'''
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 = '状态' )
19
29
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 )
22
31
self .frm_config .pack (fill = 'x' , padx = 15 , pady = 10 )
23
32
self .frm_operate .pack (fill = 'x' , padx = 15 , pady = 10 )
24
33
self .frm_status .pack (side = 'bottom' , fill = 'x' , padx = 15 , pady = 10 )
@@ -68,8 +77,11 @@ def init_config(self):
68
77
self .cfg_onedir = IntVar (value = 0 )
69
78
self .cfg_noconsole = IntVar (value = 1 )
70
79
self .cfg_clean = IntVar (value = 1 )
80
+ self .cfg_upx = IntVar (value = 1 ) # UPX 默认开启
71
81
self .cfg_rename = IntVar ()
72
82
self .cfg_exe_name = StringVar ()
83
+ # 自定义配置文件
84
+ self .cfg_specfile = StringVar (value = 'build.spec' )
73
85
# 子配置框架
74
86
self .frm_config_base = LabelFrame (
75
87
self .frm_config , text = '基本' , borderwidth = 0 )
@@ -80,11 +92,16 @@ def init_config(self):
80
92
self .frm_config_other = LabelFrame (
81
93
self .frm_config , text = '其它' , borderwidth = 0 )
82
94
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
+
83
98
# 定义按钮
84
99
self .btn_noconsole = Checkbutton (
85
100
self .frm_config_base , text = '关闭控制台' , variable = self .cfg_noconsole )
86
101
self .btn_clean = Checkbutton (
87
102
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 )
88
105
self .btn_isonefile = Checkbutton (
89
106
self .frm_config_exe , text = '独立执行文件' , variable = self .cfg_onefile )
90
107
self .btn_isonedir = Checkbutton (
@@ -94,13 +111,18 @@ def init_config(self):
94
111
self .entry_rename = Entry (
95
112
self .frm_config_other , textvariable = self .cfg_exe_name )
96
113
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
+
97
117
# 放置按钮
98
118
self .btn_isonefile .pack (side = 'left' , fill = 'x' )
99
119
self .btn_isonedir .pack (side = 'left' , fill = 'x' )
100
120
self .btn_noconsole .pack (side = 'left' , fill = 'x' )
101
121
self .btn_clean .pack (side = 'left' , fill = 'x' )
122
+ self .btn_upx .pack (side = 'left' , fill = 'x' )
102
123
self .btn_rename .pack (side = 'left' , fill = 'x' )
103
124
self .entry_rename .pack (fill = 'x' )
125
+ self .entry_specfile .pack (fill = 'x' )
104
126
105
127
# 变量自动切换操作
106
128
self .cfg_onefile .trace ('w' , self .cfg_onefile_trace )
@@ -125,10 +147,14 @@ def init_operate(self):
125
147
self .frm_operate , text = '清理' , command = self .fn_clear )
126
148
self .btn_reset = Button (
127
149
self .frm_operate , text = '重置' , command = self .fn_reset )
150
+ self .btn_advance = Button (
151
+ self .frm_operate , text = '高级选项' , command = self .fn_toggle_advance )
152
+
128
153
# 放置按钮
129
154
self .btn_build .pack (fill = 'x' , side = 'left' )
130
155
self .btn_clear .pack (fill = 'x' , side = 'left' )
131
156
self .btn_reset .pack (fill = 'x' , side = 'left' )
157
+ self .btn_advance .pack (fill = 'x' , side = 'right' )
132
158
133
159
def init_status (self ):
134
160
'''状态栏'''
@@ -137,7 +163,31 @@ def init_status(self):
137
163
138
164
def fn_build (self ):
139
165
'''生成可执行文件'''
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
141
191
142
192
def fn_clear (self ):
143
193
'''清理生成文件'''
@@ -151,9 +201,19 @@ def fn_reset(self):
151
201
self .cfg_onefile .set (1 )
152
202
self .cfg_noconsole .set (1 )
153
203
self .cfg_clean .set (1 )
204
+ self .cfg_upx .set (1 )
154
205
self .cfg_rename .set (0 )
155
206
self .cfg_exe_name .set ('' )
156
207
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
+
157
217
def fn_select_main (self ):
158
218
'''选择源文件'''
159
219
types = (
@@ -204,6 +264,8 @@ def fn_build_cmd(self):
204
264
cmds = []
205
265
cmds .append ('pyinstaller' )
206
266
cmds .append ('--windowed' )
267
+ cmds .append ('-y' )
268
+ cmds .append ('--noconfirm' )
207
269
# cmds.append('--filenames=build.spec')
208
270
# cmds.append('/usr/local/bin/pyinstaller')
209
271
@@ -216,6 +278,9 @@ def fn_build_cmd(self):
216
278
cmds .append ('--clean' )
217
279
cmds .append ('--noconfirm' )
218
280
281
+ if self .cfg_upx .get () == 0 :
282
+ cmds .append ('--noupx' )
283
+
219
284
if self .cfg_noconsole .get () == 1 :
220
285
cmds .append ('--noconsole' )
221
286
@@ -234,8 +299,8 @@ def fn_build_cmd(self):
234
299
235
300
if len (self .entry_value_list [0 ].get ()) > 0 :
236
301
cmds .append (self .entry_value_list [0 ].get ())
302
+ # print(' '.join(cmds))
237
303
return cmds
238
- # return ' '.join(cmds)
239
304
240
305
if __name__ == '__main__' :
241
306
from tkinter import Tk
0 commit comments