最近在學習wxPython時,發現img2py工具只能處理單個圖標,就本身寫了一個簡單的小工具,把文件夾下全部的圖標文件轉化到py文件裏, 話很少說,直接上代碼:python
1 # -*- coding: utf-8 -*- 2 3 # -------------------------------------------------------------------------------------------------------------------------------- 4 # Class: Img2Python 5 # Platform: WINNT 6 # Comments: Convert all ico to python file for the selected folder 7 # User: JustDoIT 8 # Date: 6th Dec, 2016 9 # -------------------------------------------------------------------------------------------------------------------------------- 10 11 import wx, os, sys, subprocess 12 import img 13 from wx.tools.img2py import img2py 14 15 # -------------------------------------------------------------------------------------------------------------------------------- 16 # Class: Img2Python 17 # -------------------------------------------------------------------------------------------------------------------------------- 18 class Img2Python(wx.Frame): 19 20 def __init__(self, *args, **kwargs): 21 super(Img2Python, self).__init__(*args, **kwargs) 22 23 self.Size = wx.Size(550, 435) 24 self.SetIcon(img.app.getIcon()) 25 26 self.InitUI() 27 28 self.Centre() 29 self.Show() 30 31 def InitUI(self): 32 33 panel = wx.Panel(self) 34 35 sizer = wx.GridBagSizer(5, 5) 36 37 text1 = wx.StaticText(panel, label="Image to python") 38 text1.SetFont(wx.Font(16, wx.DEFAULT, wx.ITALIC, wx.NORMAL)) 39 sizer.Add(text1, pos=(0, 0), span = (1, 4) ,flag = wx.LEFT | wx.TOP | wx.RIGHT, border=5) 40 41 icon = wx.StaticBitmap(panel, bitmap = img.conf32.getBitmap()) 42 sizer.Add(icon, pos=(0, 4), flag = wx.TOP | wx.RIGHT | wx.ALIGN_RIGHT, border=5) 43 44 line = wx.StaticLine(panel) 45 sizer.Add(line, pos=(1, 0), span=(1, 5), flag=wx.EXPAND | wx.BOTTOM, border=0) 46 47 # ------------------------------------------------------------------------------------------------------------------------------- 48 self.txtdir = wx.TextCtrl(panel, value = 'Select directory ...', style = wx.TE_LEFT | wx.TE_READONLY) 49 self.btndir = wx.Button(panel, wx.ID_HOME, label = 'Browser') 50 51 sizer.Add(self.txtdir, pos = (2, 0), span = (1, 4), flag = wx.LEFT | wx.TOP | wx.EXPAND, border = 5) 52 sizer.Add(self.btndir, pos = (2, 4),flag = wx.TOP | wx.EXPAND, border = 5) 53 self.btndir.Bind(wx.EVT_BUTTON, self.OnButton) 54 55 # Attribute choice ------------------------------------------------------------------------------------------------------------- 56 attbox = wx.BoxSizer(wx.HORIZONTAL) 57 nflag = wx.CheckBox(panel, label = '-n' ) 58 mflag = wx.CheckBox(panel, label = '-m' ) 59 iflag = wx.CheckBox(panel, label = '-i' ) 60 fflag = wx.CheckBox(panel, label = '-f' ) 61 62 attbox.Add(nflag, flag = wx.LEFT | wx.RIGHT, border = 5) 63 attbox.Add(mflag, flag = wx.LEFT | wx.RIGHT, border = 15) 64 attbox.Add(iflag, flag = wx.LEFT | wx.RIGHT, border = 15) 65 attbox.Add(fflag, flag = wx.LEFT | wx.RIGHT, border = 15) 66 67 sizer.Add(attbox, pos = (3, 0), span = (1, 5), flag = wx.TOP | wx.BOTTOM | wx.EXPAND, border = 0 ) 68 69 # ------------------------------------------------------------------------------------------------------------------------------ 70 sb = wx.StaticBox(panel, label = 'File Log') 71 sboxsizer = wx.StaticBoxSizer(sb, wx.VERTICAL) 72 73 self.outstr = wx.TextCtrl(panel, size = (525, 160), style = wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL | wx.VSCROLL) 74 75 sboxsizer.Add(self.outstr, proportion = 1, flag = wx.EXPAND) 76 77 sizer.Add(sboxsizer, pos = (4, 0), span =(1, 5), flag = wx.LEFT | wx.EXPAND | wx.BOTTOM, border = 5) 78 79 line2 = wx.StaticLine(panel) 80 sizer.Add(line2, pos=(6, 0), span=(1, 5), flag = wx.BOTTOM | wx.EXPAND, border = 5) 81 82 # ------------------------------------------------------------------------------------------------------------------------------ 83 btnhelp = wx.Button(panel, 301, label = 'Open') 84 btnok = wx.Button(panel, 302, label = 'OK') 85 btncancel = wx.Button(panel, 303, label = 'Cancel') 86 87 sizer.Add(btnhelp, pos=(7, 0), flag=wx.EXPAND|wx.BOTTOM, border=10) 88 sizer.Add(btnok, pos=(7, 3), flag=wx.EXPAND|wx.BOTTOM, border=10) 89 sizer.Add(btncancel, pos=(7, 4), flag=wx.EXPAND|wx.BOTTOM, border=10) 90 91 btnhelp.Bind(wx.EVT_BUTTON, self.OnButton) 92 btnok.Bind(wx.EVT_BUTTON, self.OnButton) 93 btncancel.Bind(wx.EVT_BUTTON, self.OnButton) 94 95 # ------------------------------------------------------------------------------------------------------------------------------ 96 sizer.AddGrowableCol(0) 97 sizer.AddGrowableRow(4) 98 99 panel.SetSizer(sizer) 100 101 def OnButton(self, e): 102 103 eid = e.GetId() 104 105 if eid == wx.ID_HOME: 106 107 dlg = wx.DirDialog(self, 'Select forder', style = wx.DD_DEFAULT_STYLE) 108 if dlg.ShowModal() == wx.ID_OK: 109 self.txtdir.SetValue(dlg.GetPath()) 110 print self.txtdir 111 112 dlg.Destroy() 113 114 elif eid == 301: 115 116 if os.path.isdir(self.txtdir.GetValue()): 117 subprocess.Popen('explorer ' + self.txtdir.GetValue()) 118 else: 119 wx.MessageBox('Pls select correct image folder !', 'Error', wx.OK | wx.ICON_ERROR) 120 121 elif eid == 302: 122 if os.path.isdir(self.txtdir.GetValue()): 123 self.forderprocess() 124 else: 125 wx.MessageBox('Pls select correct image folder !', 'Error', wx.OK | wx.ICON_ERROR) 126 elif eid == 303: 127 self.Close() 128 else: e.Skip() 129 130 131 def forderprocess(self): 132 folder = self.txtdir.GetValue() 133 listext = ('.png', '.ico', '.icon', '.gif') 134 135 pyfile = folder + '\img.py' 136 137 if os.path.isdir(folder): 138 139 self.outstr.AppendText('Starting.................................................................................\n\n') 140 141 for name in os.listdir(folder): 142 (x, ext) = os.path.splitext(name) 143 144 if ext in listext: 145 img = os.path.join(folder, name) 146 147 fs = open('log.txt', 'w') 148 temp = sys.stdout 149 sys.stdout = fs 150 151 self.outstr.AppendText('Name :' + x) 152 153 if os.path.isfile(pyfile): 154 ret = img2py(img, pyfile, append = True) 155 else: 156 ret = img2py(img, pyfile, append = True) 157 158 sys.stdout = temp 159 fs.close() 160 str = open('log.txt','r').read() 161 self.outstr.AppendText(str + '\n') 162 163 self.outstr.AppendText('Done ....................................................................................\n') 164 165 166 # -------------------------------------------------------------------------------------------------------------------------------- 167 # Class: APP 168 # -------------------------------------------------------------------------------------------------------------------------------- 169 class App(wx.App): 170 171 def OnInit(self): 172 defaultstyle = wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN | wx.FRAME_NO_TASKBAR 173 Img2Python(None, title = 'Image To Python', style = defaultstyle) 174 return True 175 176 # -------------------------------------------------------------------------------------------------------------------------------- 177 # Function main() 178 # -------------------------------------------------------------------------------------------------------------------------------- 179 def main(): 180 App().MainLoop() 181 182 if __name__ == '__main__': 183 main()
效果圖:app