wxpython 中的派生面板 类
Derived panel classes in wxpython
在我的 wxpython 程序中,我的面板的行为有所不同,具体取决于我是将其设为派生 class 还是直接面板实例:
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 的四行并使用派生的 class 取消注释创建 panel1 的行,"Some Text" 被剪掉并且只显示 "Sor"。当我编写 non-trivial 程序时,更糟糕的事情开始发生。
这两个在我看来是一样的。有什么区别?
我正在使用:
Python2.7.6
wxpython 3.0.0.0
Mac Yosemite 10.10.2
问题出在育儿上。问题在于,在第一个示例中,您将 StaticText
小部件的父级正确设置为 panel1
。在您的 PanelWithText
class 中,您将其父级设置为顶级面板而不是面板 class,这是不正确的。这是一个固定的例子:
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()
在我的 wxpython 程序中,我的面板的行为有所不同,具体取决于我是将其设为派生 class 还是直接面板实例:
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 的四行并使用派生的 class 取消注释创建 panel1 的行,"Some Text" 被剪掉并且只显示 "Sor"。当我编写 non-trivial 程序时,更糟糕的事情开始发生。
这两个在我看来是一样的。有什么区别?
我正在使用: Python2.7.6 wxpython 3.0.0.0 Mac Yosemite 10.10.2
问题出在育儿上。问题在于,在第一个示例中,您将 StaticText
小部件的父级正确设置为 panel1
。在您的 PanelWithText
class 中,您将其父级设置为顶级面板而不是面板 class,这是不正确的。这是一个固定的例子:
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()