[Video] wxFormBuilder: Build a Python Gif Viewer – 5
In the last tutorial you created a python script that would fix up some of the wxFormBuilder code. In this tutorial you will create a new frameFixCleaner.py script that cleans up unwanted code and adds a main loop so your application will work straight away.
This is a very handy tool and as you will notice it speeds up your time in creating python GUI applications using wxFormBuilder.
Here is the 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 |
# 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.' |
If you have any questions or comments please post them on my YouTube video.
Next Tutorial: [Video] wxFormBuilder: Build a Python Gif Viewer – 6