I am rather new to wx/python so please excuse if this is stupid or ill described.
I am substituting a nested sizer with a new one as shown in the snippet below. after 开发者_如何学编程some tinkering everything seems to work out but the re-drawing of the parent-sizer. the content of the old nested sizer remains and gets "painted" over with the new sizer content despite my sizer.Layout()
system setup:
- python 2.5.5.2 and 2.6.4 - wxpython 2.8# -*- coding: utf8 -*-
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title='test')
class Test(wx.App):
def OnInit(self):
frame = Frame()
self.panel = wx.Panel(frame)
self.panel.SetBackgroundColour('red')
self.sizer = wx.BoxSizer(wx.VERTICAL)
button = wx.Button(self.panel, wx.ID_ANY, 'TEST')
self.hsizer = wx.BoxSizer(wx.HORIZONTAL)
self.hsizer.Add(wx.StaticText(self.panel, wx.ID_ANY, 'nacknack'))
self.sizer.Add(button)
self.Bind(wx.EVT_BUTTON, self.on_test_button, button)
self.text = wx.StaticText(self.panel, wx.ID_ANY, 'FOOO')
self.sizer.Add(self.text)
self.sizer.Add(self.hsizer)
self.panel.SetSizer(self.sizer)
frame.Show()
return True
def on_test_button(self, evt):
tmpsizer = wx.BoxSizer(wx.VERTICAL)
tmpsizer.Add(self.makesizer())
tmpitem = tmpsizer.GetChildren()[0]
self.sizer.Replace(2, tmpitem)
self.sizer.Layout()
def makesizer(self):
testsizer = wx.BoxSizer(wx.HORIZONTAL)
testsizer.Add(wx.StaticText(self.panel, wx.ID_ANY, 'testsizer'))
return testsizer
if __name__ == '__main__':
app = Test()
app.MainLoop()
After looking through your code (and it wasn't easy - please consider formatting it differently next time - maybe grouping the self.sizer.add
functions or something) I think I discovered your bug:
When you call Replace
on a sizer, the item being replaced is not destroyed but becomes no longer managed by the sizer. You need to hide or destroy the old window (the wx.StaticText
)
As mentioned in the wxWidgets docs:
virtual bool Replace (wxWindow *oldwin, wxWindow *newwin, bool recursive=false)
Detaches the given oldwin from the sizer and replaces it with the given newwin.
精彩评论