[Video] wxFormBuilder: Build a Python Gif Viewer – 9 listbox Code
We are now getting closer to finishing the Gif Viewer using python, wxpython and wxFormBuilder.
Now we add the code to populate the listbox with all Gif images found in a directory.
In this tutorial we change the file name that is converted from the descode directory to filenameBK.py
Changed frameFixCleaner.py code with filenameBK.py
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.' |
part of the code for populating the listbox with gif files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
self.GifPath = None # ------------ Call Populates self.Show() # ------------ Event handlers def dirPickerDirOnDirChanged( self, event ): self.GifPath = event.GetPath() self.popGifList() def popGifList(self): self.listBoxGifiles.Clear() allFiles = os.listdir(self.GifPath) for file in allFiles: if file.endswith('.gif'): self.listBoxGifiles.Append(file) |
If you have any questions or comments please post them on my YouTube video
Also,If you liked this video click the like button and subscribe to Python Bytes channel
Next Tutorial: [Video] wxFormBuilder: Build a Python Gif Viewer – 10 Gif Animator