[Video] wxFormBuilder: Build a Python Gif Viewer – 7
Our seventh tutorial goes through the frameFixCleaner.py code to cleans up the wxFormBuilder generated python code and adds a main loop so we can run the application. In this tutorial I rehash the code to get it just right and working.
Here is the completed frameFixCleaner.py code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# frameFixCleaner.py # # By Daryl Williams # www.fandangleproductions.com import os def cleanFrame(fmcode): cleancode = fmcode.replace('\t',' ') cleancode = cleancode.replace('m_','') cleancode = cleancode.replace(' # Virtual event handlers, overide them in your derived class','') return cleancode def fixFrame(prjDir,filename): fmcode="" srcFile = os.path.join(prjDir+'\descode',filename+'.py') f=open(srcFile,'r') for line in f: if line.startswith(('# -*- coding: utf-8 -*- ')): fmcode = fmcode + "# %s.py" % filename elif line.startswith(('import wx.xrc')): pass elif line.startswith(('##')): pass elif line.startswith(('class')): lineSp = line.split(' ') frameName = lineSp[1] fmcode = fmcode + line elif line.startswith((' def __init__( self, parent ):')): fmcode = fmcode + ' def __init__( self ):\n' elif line.startswith((' wx.Frame.__init__')): line = line.replace('parent','None') fmcode = fmcode + line elif line.startswith((' def __del__( self ):')): pass elif line.startswith((' pass')): fmcode = fmcode + ' # ------------ Add widget program settings\n\n' fmcode = fmcode + ' # ------------ Call Populates\n\n' fmcode = fmcode + ' self.Show()\n\n' fmcode = fmcode + ' # ------------ Event handlers' else: fmcode = fmcode + line # ----- add main loop fmcode = fmcode + 'if __name__ == "__main__":\n' fmcode = fmcode + ' app = wx.App(False)\n' fmcode = fmcode + ' frame = %s()\n' % frameName fmcode = fmcode + ' app.MainLoop()\n' #print fmcode f.close() cleancode = cleanFrame(fmcode) outFile = os.path.join(prjDir+'\src',filename+'BK.py') f=open(outFile,'w') f.write(cleancode) f.close() prjDir = r"C:\Python27\wxFormBuilder\fb-templates\gif-viewer" filename = "gifviewer" print 'Cleaning descode file: %s' % (filename+'BK.py') outStat = fixFrame(prjDir,filename) print 'Clean descode file and save in src folder %s.' |
Next Tutorial: [Video] wxFormBuilder: Build a Python Gif Viewer- 8 listbox