Attachment 'D__Data_Python_py2exe_gui_py2exe_gui.pyw'

Download

Toggle line numbers
   1 # -*- coding: utf-8 -*-
   2 
   3 #	Programmer:	0.707	
   4 #   $Id: py2exe_gui.pyw
   5 #   $Ver: 0.03
   6 
   7 import sys
   8 import os
   9 import wx
  10 import  wx.grid as  gridlib
  11 
  12 from  py2exe.build_exe import py2exe
  13 from distutils.core import Distribution
  14 
  15 exe_typs = ['console', 'windows','service','com_server']
  16 
  17 def MakeExe(dir,par_d,opt_d):    
  18     os.chdir(dir)
  19     sys_old_path = sys.path[:]  
  20     sys.path.insert(0,dir)
  21     
  22     dist = Distribution(par_d) 
  23     cmd = py2exe(dist)
  24         
  25     cmd.__dict__.update(opt_d) 
  26 
  27     cmd.ensure_finalized()        
  28     cmd.run()        
  29     sys.path = sys_old_path        
  30         
  31     build_dir = os.path.join(dir,'build')        
  32     for root, dirs, files in os.walk(build_dir, topdown=False):
  33         for name in files:
  34             os.remove(os.path.join(root, name))
  35         for name in dirs:
  36             os.rmdir(os.path.join(root, name))
  37     os.rmdir(build_dir)
  38 
  39 
  40  
  41 class PyFileGrid(gridlib.Grid):
  42     def __init__(self, parent):
  43         gridlib.Grid.__init__(self, parent, -1,style=wx.STATIC_BORDER)
  44         self.CreateGrid(1, 2)             
  45         self.SetColLabelValue(0,u"文件名")
  46         self.SetColLabelValue(1,u"程序类型")
  47         self.SetColSize(0,200)
  48         self.SetColSize(1,100)
  49         self.SetRowLabelSize(30)
  50         self.DeleteRows()  
  51               
  52     def CreateEditor(self):
  53         return gridlib.GridCellChoiceEditor(exe_typs, False)        
  54         
  55     def SetValues(self,files): 
  56         row_c = self.GetNumberRows()
  57         if row_c:
  58             self.DeleteRows(0,row_c)
  59         file_c = len(files)
  60         self.AppendRows(file_c)
  61         for row,file in enumerate(files):
  62             self.SetCellEditor(row, 1, self.CreateEditor())
  63             self.SetCellValue(row,0,file)
  64             ext = os.path.splitext(file)[1].lower()
  65             if ext =='.pyw':
  66                 self.SetCellValue(row,1,'windows')
  67             elif ext =='.py':
  68                 self.SetCellValue(row,1,'console')
  69                 
  70     def GetValueDict(self):        
  71         row_c = self.GetNumberRows()
  72         v_d = {}        
  73         for row in range(row_c):
  74             typ = self.GetCellValue(row,1)
  75             if typ in exe_typs:
  76                 if typ not in v_d:
  77                     v_d[typ] = []
  78                 v_d[typ].append(self.GetCellValue(row,0))
  79         return v_d
  80             
  81             
  82 
  83 
  84 class DataFileGrid(gridlib.Grid):
  85     def __init__(self, parent):
  86         gridlib.Grid.__init__(self, parent, -1,style=wx.STATIC_BORDER)
  87         self.CreateGrid(1, 2)             
  88         self.SetColLabelValue(0,u"源文件")
  89         self.SetColLabelValue(1,u"安装目录")
  90         self.SetColSize(0,300)
  91         self.SetColSize(1,60)
  92         self.SetRowLabelSize(20)
  93         self.DeleteRows() 
  94         self.SetSelectionMode(1)
  95         
  96     def SetValues(self,paths):
  97         row_c = self.GetNumberRows()           
  98         file_c = len(paths)            
  99         self.AppendRows(file_c)
 100             
 101         for i,p in enumerate(paths):
 102             self.SetCellValue(row_c+i,0,p)
 103             self.SetCellValue(row_c+i,1,'data') 
 104             
 105     def DelSelectedRows(self):       
 106         tls = self.GetSelectionBlockTopLeft()
 107         brs = self.GetSelectionBlockBottomRight()
 108 
 109         needdel_rows = []
 110         for t,b in [(x[0],y[0]) for x,y in zip(tls,brs)]:
 111             needdel_rows += range(t,b+1)
 112         
 113         old_row_c = self.GetNumberRows()
 114         new_row_c = old_row_c-len(needdel_rows)
 115         
 116         remain_rows = []
 117         for row in  range(old_row_c):
 118             if row not in needdel_rows:
 119                 remain_rows.append((self.GetCellValue(row,0),self.GetCellValue(row,1)))
 120         
 121         self.DeleteRows(0,old_row_c)
 122         self.AppendRows(new_row_c)        
 123         
 124         for i,fd in enumerate(remain_rows):
 125             self.SetCellValue(i,0,fd[0])
 126             self.SetCellValue(i,1,fd[1])
 127     
 128     def GetValueDict(self):
 129         row_c = self.GetNumberRows()
 130         v_d = {}        
 131         for row in range(row_c):
 132             path = self.GetCellValue(row,1).encode('utf-8')
 133             file = self.GetCellValue(row,0).encode('utf-8')
 134             if path not in v_d:
 135                 v_d[path] = []
 136             if len(file):
 137                 v_d[path].append(file)
 138         return v_d 
 139 
 140 class MyFrame(wx.Frame):
 141     def __init__(self):
 142         wx.Frame.__init__(self,None,-1,u"PY2EXE GUI",style=wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX ,size=wx.Size(400,400))
 143      
 144         # Prepare the menu bar
 145         menuBar = wx.MenuBar()
 146 
 147         # 1st menu from left
 148         menu = wx.Menu()
 149         
 150         #menu.Append(101, u"打开", u"打开一个已有工程")
 151         #menu.Append(102, u"保存", u"把当前工程信息存盘")
 152         #menu.Append(103, u"另存为...", u"把当前工程信息换名存盘")
 153         menu.AppendSeparator()
 154         menu.Append(104, u"退出", u"退出本程序")
 155         # Add menu to the menu bar
 156         menuBar.Append(menu, u"工程")
 157         menu = wx.Menu()
 158         
 159         menu.Append(201, u"生成可执行文件", u"")
 160         menuBar.Append(menu, u"py2exe")
 161         
 162         
 163         self.SetMenuBar(menuBar)
 164         
 165         self.Bind(wx.EVT_MENU, self.Exit, id=104)
 166         self.Bind(wx.EVT_MENU, self.OnMakeExe,id=201 )
 167         
 168         #notebook
 169         self.nb = nb = wx.Notebook(self,-1,style=wx.NB_BOTTOM )
 170 
 171         #页面1,基本信息,选择要生成的控制台程序和窗口程程序脚本
 172         self.panel_1 = panel_1 = wx.Panel(nb, -1,style=wx.STATIC_BORDER)        
 173         self.nb.AddPage(panel_1,u"程序文件",select = True)        
 174   
 175            
 176         self.help_label = wx.StaticText(panel_1, -1, u"  点右边按扭选择要打包的python文件(可多选),再点菜单'py2exe'->'生成可执行文件'既可.",size=(240,50))
 177         self.add_py_btn = wx.Button(panel_1,-1, u"选择python文件",size=(100,40))      
 178 
 179         self.dir_label = wx.StaticText(panel_1, -1, u"目录:")
 180         self.dir_text = wx.TextCtrl(panel_1, -1, u"",style=wx.TE_READONLY )
 181                 
 182         self.py_grid = PyFileGrid(self.panel_1)
 183         
 184         self.Bind(wx.EVT_BUTTON, self.AddPyFile,self.add_py_btn)    
 185         
 186          #页面3,数据文件
 187         self.panel_3 = panel_3 = wx.Panel(nb, -1,style=wx.STATIC_BORDER)        
 188         self.nb.AddPage(panel_3,u"数据文件")
 189 
 190         self.add_data_btn = wx.Button(panel_3,-1, u"添加",size=(40,24))
 191         self.del_data_btn = wx.Button(panel_3,-1, u"删除",size=(40,24))
 192         
 193         self.data_grid = DataFileGrid(self.panel_3)
 194         
 195         self.Bind(wx.EVT_BUTTON, self.AddDataFile,self.add_data_btn)
 196         self.Bind(wx.EVT_BUTTON, self.DelDataFile,self.del_data_btn)
 197         
 198         #页面2,选项
 199         self.panel_2 = panel_2 = wx.Panel(nb, -1,style=wx.STATIC_BORDER )
 200         self.nb.AddPage(panel_2,u"py2exe选项")
 201         
 202         self.optimize_label = wx.StaticText(panel_2, -1, u"optimization level(优化选项):")       
 203         self.optimize_box = wx.ComboBox(panel_2,-1,value='0',choices = ['0','1','2'],style = wx.CB_READONLY )
 204         
 205         self.distdir_label = wx.StaticText(panel_2, -1, u"dist-dir(存放生成文件的目录):")       
 206         self.distdir_box = wx.TextCtrl(panel_2,-1,u'dist',size=(200,22))
 207         
 208         self.excludes_label = wx.StaticText(panel_2, -1, u'excludes(排除的模块,以","分隔):')
 209         self.excludes_box = wx.TextCtrl(panel_2,-1,u'',size=(183,22))
 210         
 211         self.ignores_label = wx.StaticText(panel_2, -1, u'ignores(忽略的模块,以","分隔):')       
 212         self.ignores_box = wx.TextCtrl(panel_2,-1,u'',size=(190,22))
 213         
 214         self.includes_label = wx.StaticText(panel_2, -1, u'includes(包含的模块,以","分隔):')       
 215         self.includes_box = wx.TextCtrl(panel_2,-1,u'',size=(185,22))
 216         
 217         self.packages_label = wx.StaticText(panel_2, -1, u'packages(包含的模块包,以","分隔):')       
 218         self.packages_box = wx.TextCtrl(panel_2,-1,u'',size=(168,22))
 219         
 220         self.bundle_label = wx.StaticText(panel_2, -1, u'bundle-files(打包dll级别):')       
 221         self.bundle_box = wx.ComboBox(panel_2,-1,value='3',choices = ['1','2','3'],style = wx.CB_READONLY )
 222         
 223         
 224         self.compressed = wx.CheckBox(panel_2,-1,u"compressed(生成压缩文件zipfile)")
 225         self.xref = wx.CheckBox(panel_2,-1,u"xref(生成模块交叉引用)")
 226         self.ascii = wx.CheckBox(panel_2,-1,u"ascii(不自动包含encoding和codecs)")
 227 
 228 
 229         
 230         #页面3,py2exe输出
 231         self.output = wx.TextCtrl(nb, -1,style=wx.TE_MULTILINE)
 232         self.nb.AddPage(self.output,u"py2exe输出")
 233         
 234 
 235         self.__do_layout()
 236 
 237         self.Bind(wx.EVT_CLOSE,self.OnCloseWindow,self) 
 238 
 239         self.lasts = ''
 240   
 241     def __do_layout(self):
 242        
 243         sizer = wx.BoxSizer(wx.VERTICAL)
 244         
 245         sizer_0 = wx.BoxSizer(wx.HORIZONTAL)
 246         sizer_1 = wx.BoxSizer(wx.HORIZONTAL)        
 247         sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
 248         sizer_11 = wx.BoxSizer(wx.VERTICAL)        
 249         sizer_12 = wx.BoxSizer(wx.VERTICAL)       
 250         
 251         
 252         sizer_0.Add(self.dir_label,0, wx.LEFT|wx.RIGHT, 5)        
 253         sizer_0.Add(self.dir_text, 1,wx.RIGHT|wx.EXPAND, 10)
 254              
 255        
 256         
 257         sizer_1.Add(self.py_grid,1, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 10)  
 258        
 259         
 260         sizer_2.Add(self.help_label, 0, wx.LEFT|wx.RIGHT,10)
 261         sizer_2.Add(self.add_py_btn, 1, wx.LEFT|wx.RIGHT,10)
 262         
 263         sizer.Add(sizer_2, 0,wx.TOP|wx.BOTTOM,20)
 264         sizer.Add(sizer_0, 0,wx.TOP|wx.EXPAND,10)
 265         sizer.Add(sizer_1, 1,wx.TOP|wx.EXPAND,10)
 266            
 267         
 268         self.panel_1.SetAutoLayout(1)
 269         self.panel_1.SetSizer(sizer)       
 270         
 271         
 272         
 273         sizer1 = wx.BoxSizer(wx.VERTICAL)
 274         
 275 
 276         sizer1_1 = wx.BoxSizer(wx.HORIZONTAL)        
 277         sizer1_2 = wx.BoxSizer(wx.HORIZONTAL)
 278         sizer1_3 = wx.BoxSizer(wx.HORIZONTAL)        
 279         sizer1_4 = wx.BoxSizer(wx.HORIZONTAL)
 280         sizer1_5 = wx.BoxSizer(wx.HORIZONTAL)        
 281         sizer1_6 = wx.BoxSizer(wx.HORIZONTAL)
 282         sizer1_7 = wx.BoxSizer(wx.HORIZONTAL)        
 283         sizer1_8 = wx.BoxSizer(wx.HORIZONTAL)
 284         
 285         sizer1_1.Add(self.optimize_label,0, wx.LEFT|wx.RIGHT, 5)  
 286         sizer1_1.Add(self.optimize_box,1, wx.LEFT|wx.RIGHT, 5) 
 287         
 288         sizer1_2.Add(self.distdir_label,0, wx.LEFT|wx.RIGHT, 5)  
 289         sizer1_2.Add(self.distdir_box,1, wx.LEFT|wx.RIGHT, 5) 
 290         
 291         sizer1_3.Add(self.excludes_label,0, wx.LEFT|wx.RIGHT, 5)  
 292         sizer1_3.Add(self.excludes_box,1, wx.LEFT|wx.RIGHT, 5) 
 293         
 294         sizer1_4.Add(self.ignores_label,0, wx.LEFT|wx.RIGHT, 5)  
 295         sizer1_4.Add(self.ignores_box,1, wx.LEFT|wx.RIGHT, 5) 
 296         
 297         sizer1_5.Add(self.includes_label,0, wx.LEFT|wx.RIGHT, 5)  
 298         sizer1_5.Add(self.includes_box,1, wx.LEFT|wx.RIGHT, 5) 
 299         
 300         sizer1_6.Add(self.packages_label,0, wx.LEFT|wx.RIGHT, 5)  
 301         sizer1_6.Add(self.packages_box,1, wx.LEFT|wx.RIGHT, 5) 
 302         
 303         sizer1_7.Add(self.bundle_label,0, wx.LEFT|wx.RIGHT, 5)  
 304         sizer1_7.Add(self.bundle_box,1, wx.LEFT|wx.RIGHT, 5) 
 305         
 306         sizer1_8.Add(self.compressed,0, wx.LEFT|wx.RIGHT, 5)  
 307         sizer1_8.Add(self.xref,0, wx.LEFT|wx.RIGHT, 5) 
 308         
 309         
 310         
 311         sizer1.Add(sizer1_1, 0,wx.TOP|wx.BOTTOM,5)
 312         sizer1.Add(sizer1_2, 0,wx.TOP|wx.BOTTOM,5)
 313         sizer1.Add(sizer1_3, 0,wx.TOP|wx.BOTTOM,5)
 314         sizer1.Add(sizer1_4, 0,wx.TOP|wx.BOTTOM,5)
 315         sizer1.Add(sizer1_5, 0,wx.TOP|wx.BOTTOM,5)
 316         sizer1.Add(sizer1_6, 0,wx.TOP|wx.BOTTOM,5)
 317         sizer1.Add(sizer1_7, 0,wx.TOP|wx.BOTTOM,5)
 318         sizer1.Add(sizer1_8, 0,wx.TOP|wx.BOTTOM,5)
 319         sizer1.Add(self.ascii,0, wx.LEFT|wx.RIGHT, 5) 
 320         self.panel_2.SetAutoLayout(1)
 321         self.panel_2.SetSizer(sizer1)
 322         
 323         
 324         sizer2 = wx.BoxSizer(wx.VERTICAL)
 325         sizer2_1 = wx.BoxSizer(wx.HORIZONTAL) 
 326         
 327         sizer2_1.Add(self.add_data_btn, 1, wx.LEFT|wx.RIGHT,50)
 328         sizer2_1.Add(self.del_data_btn,1, wx.LEFT|wx.RIGHT,50)
 329                    
 330         sizer2.Add(sizer2_1, 0, wx.TOP|wx.BOTTOM,10)
 331         sizer2.Add(self.data_grid,1,wx.EXPAND,0)
 332          
 333         self.panel_3.SetAutoLayout(1)
 334         self.panel_3.SetSizer(sizer2)
 335         
 336         
 337         
 338     def AddDataFile(self,evt):
 339         dlg = wx.FileDialog(
 340             self, message=u"选择文件", defaultDir="",wildcard=u"数据文件(*.*)|*.*",
 341             style=wx.OPEN | wx.CHANGE_DIR|wx.MULTIPLE
 342             )
 343        
 344         if dlg.ShowModal() == wx.ID_OK:
 345             self.data_grid.SetValues(dlg.GetPaths())
 346         dlg.Destroy()
 347     
 348     def DelDataFile(self,evt):
 349         self.data_grid.DelSelectedRows()
 350 
 351     
 352     def AddPyFile(self,evt):
 353         dlg = wx.FileDialog(
 354             self, message=u"选择文件", defaultDir="",wildcard=u"Python文件(*.py,*.pyw)|*.py;*.pyw",
 355             style=wx.OPEN | wx.CHANGE_DIR|wx.MULTIPLE
 356             )
 357        
 358         if dlg.ShowModal() == wx.ID_OK:
 359             self.dir_text.SetValue(dlg.GetDirectory())                        
 360             self.py_grid.SetValues(dlg.GetFilenames())         
 361                                           
 362         dlg.Destroy()
 363     
 364     
 365     def OnMakeExe(self,evt):
 366         self.output.Clear()
 367         self.nb.SetSelection(3)
 368         self.lasts = ''
 369         
 370         dir = self.dir_text.GetValue()
 371         par_d = self.GetPar()
 372         opt_d = self.GetOpt()
 373         
 374         MakeExe(dir,par_d,opt_d)
 375        
 376         
 377     def GetPar(self):
 378         par_d = {}
 379         par_d["zipfile"] = "library.zip"
 380         par_d["ctypes_com_server"]= []
 381         par_d["com_server"] = []
 382         par_d["service"] = []
 383         par_d["console"] =[]
 384         par_d["isapi"] = []
 385         par_d["windows"] = []
 386         par_d["console"] = []
 387         
 388         par_d.update(self.py_grid.GetValueDict())               
 389         par_d["data_files"] = self.data_grid.GetValueDict().items()        
 390         
 391         return par_d
 392     
 393     def GetOpt(self):
 394         opt_d = {}
 395          
 396         opt_d['optimize'] = self.optimize_box.GetValue().encode('utf-8')
 397         opt_d['bundle_files'] = self.bundle_box.GetValue().encode('utf-8')
 398         
 399         dist_dir = self.distdir_box.GetValue().encode('utf-8')
 400         if dist_dir != '':
 401             opt_d['dist_dir'] = dist_dir
 402         
 403         excludes = self.excludes_box.GetValue().encode('utf-8')
 404         if excludes != "":
 405             opt_d['excludes'] = excludes
 406             
 407         ignores = self.ignores_box.GetValue().encode('utf-8')
 408         if ignores!= "":
 409             opt_d['ignores'] = ignores
 410         
 411         includes = self.includes_box.GetValue().encode('utf-8')
 412         if includes != "":
 413             opt_d['includes'] = includes
 414         
 415         packages = self.packages_box.GetValue().encode('utf-8')
 416         if packages != "":
 417             opt_d['packages'] = packages
 418         
 419         
 420         opt_d['compressed'] = int(self.compressed.GetValue())
 421         opt_d['xref'] = int(self.xref.GetValue())
 422         opt_d['ascii'] = int(self.ascii.GetValue())
 423         
 424         return opt_d
 425         
 426         
 427     def Exit(self,evt):
 428         self.Close(True)
 429           
 430     def OnCloseWindow(self, event):
 431         self.Destroy()
 432         
 433     def write(self,s):
 434         pre = s[:8]
 435         if pre=="byte-com" or pre=="skipping":
 436             return
 437         if s == self.lasts:
 438             return
 439         
 440         self.lasts = s
 441         self.output.AppendText(s)
 442 
 443 # end of class MyFrame
 444 
 445 class MyApp(wx.App):
 446     def OnInit(self):
 447         frame_1 = MyFrame()
 448         frame_1.Show(True)
 449         sys.stdout = frame_1
 450         #sys.stderr = frame_1
 451         self.SetTopWindow(frame_1)
 452         return True
 453 
 454 if __name__ == "__main__":
 455     app = MyApp(0)        
 456     app.MainLoop()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-05-11 08:51:49, 15.9 KB) [[attachment:D__Data_Python_py2exe_gui_py2exe_gui.pyw]]
  • [get | view] (2021-05-11 08:51:49, 15.9 KB) [[attachment:py2exe_gui.pyw]]
  • [get | view] (2021-05-11 08:51:49, 16.4 KB) [[attachment:py2exe_gui_V0.04.pyw]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.