Derived panel classes in wxpython
在wxpython程序中,面板的行为不同,这取决于它是派生类还是直接面板实例:
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 | import wx class PanelWithText(wx.Panel): def __init__(self, parent): super(PanelWithText, self).__init__(parent) hbox1 = wx.BoxSizer(wx.HORIZONTAL) panel1 = wx.Panel(parent) st1 = wx.StaticText(panel1, label='Some Text') hbox1.Add(st1) class Example(wx.Frame): def __init__(self, parent, title): super(Example, self).__init__(parent, title=title, size=(390, 350)) panel = wx.Panel(self) vbox = wx.BoxSizer(wx.VERTICAL) hbox1 = wx.BoxSizer(wx.HORIZONTAL) # comment out from here panel1 = wx.Panel(panel) # st1 = wx.StaticText(panel1, label='Some Text') # hbox1.Add(st1) # to here # panel1 = PanelWithText(panel) vbox.Add(panel1) panel.SetSizer(vbox) self.Centre() self.Show() if __name__ == '__main__': app = wx.App() Example(None, title='Example') app.MainLoop() |
如果我按原样运行,它看起来很好。如果我运行它注释掉创建panel1的四行,并取消注释使用派生类创建panel1的行,那么"some text"将被剪切并只显示"sor"。当我做了一个不平凡的程序时,更糟糕的事情就开始发生了。
我觉得这两个完全一样。有什么区别?
我正在使用:Python 2.7.6Wxpython 3.0.0.0版麦约塞米蒂10.10.2
问题在于养育孩子。问题在于,在第一个示例中,您将
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 | import wx class PanelWithText(wx.Panel): def __init__(self, parent): super(PanelWithText, self).__init__(parent) hbox1 = wx.BoxSizer(wx.HORIZONTAL) st1 = wx.StaticText(self, label='Some Text') hbox1.Add(st1) class Example(wx.Frame): def __init__(self, parent, title): super(Example, self).__init__(parent, title=title, size=(390, 350)) panel = wx.Panel(self) vbox = wx.BoxSizer(wx.VERTICAL) #hbox1 = wx.BoxSizer(wx.HORIZONTAL) # comment out from here #panel1 = wx.Panel(panel) # #st1 = wx.StaticText(panel1, label='Some Text') # #hbox1.Add(st1) # to here panel1 = PanelWithText(panel) vbox.Add(panel1) panel.SetSizer(vbox) self.Centre() self.Show() if __name__ == '__main__': import wx.lib.mixins.inspection app = wx.App() Example(None, title='Example') wx.lib.inspection.InspectionTool().Show() app.MainLoop() |