I’m creating a small wxPython utility for the first time, and I’m stuck on a problem.
I would like to add components to an already created frame. To do this, I am destroying the frame’s old panel, and creating a new panel with all new components.
1: Is there a better way of dynamically adding content to a panel?
2: Why, in the following example, do I get a a strange redraw error in which in the panel is drawn only in the top left hand corner, and when resized, the panel is drawn correctly? (WinXP, Python 2.5, latest wxPython)
Thank you for the help!
import wx class MainFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, 'TimeTablr') #Variables self.iCalFiles = ['Empty', 'Empty', 'Empty'] self.panel = wx.Panel(self, -1) self.layoutElements() def layoutElements(self): self.panel.Destroy() self.panel = wx.Panel(self, -1) #Buttons self.getFilesButton = wx.Button(self.panel, 1, 'Get Files') self.calculateButton = wx.Button(self.panel, 2, 'Calculate') self.quitButton = wx.Button(self.panel, 3, 'Quit Application') #Binds self.Bind(wx.EVT_BUTTON, self.Quit, id=3) self.Bind(wx.EVT_BUTTON, self.getFiles, id=1) #Layout Managers vbox = wx.BoxSizer(wx.VERTICAL) #Panel Contents self.ctrlsToDescribe = [] self.fileNames = [] for iCalFile in self.iCalFiles: self.ctrlsToDescribe.append(wx.TextCtrl(self.panel, -1)) self.fileNames.append(wx.StaticText(self.panel, -1, iCalFile)) #Add Components to Layout Managers for i in range(0, len(self.ctrlsToDescribe)): hboxtemp = wx.BoxSizer(wx.HORIZONTAL) hboxtemp.AddStretchSpacer() hboxtemp.Add(self.fileNames[i], 1, wx.EXPAND) hboxtemp.AddStretchSpacer() hboxtemp.Add(self.ctrlsToDescribe[i], 2, wx.EXPAND) hboxtemp.AddStretchSpacer() vbox.Add(hboxtemp) finalHBox = wx.BoxSizer(wx.HORIZONTAL) finalHBox.Add(self.getFilesButton) finalHBox.Add(self.calculateButton) finalHBox.Add(self.quitButton) vbox.Add(finalHBox) self.panel.SetSizer(vbox) self.Show() def Quit(self, event): self.Destroy() def getFiles(self, event): self.iCalFiles = ['Example1','Example1','Example1','Example1','Example1','Example1'] self.layoutElements() self.Update() app = wx.App() MainFrame() app.MainLoop() del app
1) I beleive the Sizer will let you insert elements into the existing ordering of them. That would probably be a bit faster.
2) I don’t see the behavior you’re describing on OSX, but at a guess, try calling self.Layout() before self.Show() in layoutElements?