我是wxpython的新手,并且一直试图在书籍(树书,笔记本,选择书)中放置一些小部件。我经常最终将一些小部件放在容器内,而不是响应事件。我不确定我做错了什么。下面是我的代码之一
import wx
class ChoicePanelTwo(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.SetBackgroundColour('blue')
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.List = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
for i in range(seed):
self.List.InsertStringItem(i, str(i))
sizer.Add(self.List, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class ChoicePanelOne(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.SetBackgroundColour('green')
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.RegisterList = wx.Choicebook(self, wx.ID_ANY)
sizer.Add(self.RegisterList, 1, wx.ALL|wx.EXPAND, 5)
for i in range(seed):
self.RegisterList.AddPage(ChoicePanelTwo(self, seed*50), str(i))
self.SetSizer(sizer)
class TreePanel(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
self.SetBackgroundColour('cyan')
self.Choicbook = wx.Choicebook(self, wx.ID_ANY)
for i in range(seed):
self.Choicbook.AddPage(ChoicePanelOne(self, seed*2), str(i))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.Choicbook, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class AppFrame(wx.Frame):
""" The main frame of the application
"""
title = 'Application'
WindowSize = (1024, 768)
seed = 2
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title, size=self.WindowSize, style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
self.create_main_panel()
def create_main_panel(self):
self.panel = TreePanel(self, self.seed)
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = AppFrame()
app.frame.Show()
app.MainLoop()
在这个例子中。选择书和清单似乎确实有效。我做错了什么?
答案 0 :(得分:2)
这是一个育儿问题。您将所有ChoicePanelOne实例的父项设置为TreePanel,它应该是ChoicBook。你在ChoicePanelOne做同样的事情,在那里你创建了一大堆ChoicePanelTwo,当它们应该是RegisterList时,它们的父母是ChoicePanelOne。查看下面稍微更改过的代码:
import wx
class ChoicePanelTwo(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.SetBackgroundColour('blue')
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.List = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
for i in range(seed):
self.List.InsertStringItem(i, str(i))
sizer.Add(self.List, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class ChoicePanelOne(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.SetBackgroundColour('green')
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.RegisterList = wx.Choicebook(self, wx.ID_ANY)
sizer.Add(self.RegisterList, 1, wx.ALL|wx.EXPAND, 5)
for i in range(seed):
self.RegisterList.AddPage(ChoicePanelTwo(self.RegisterList, seed*50), str(i))
self.SetSizer(sizer)
class TreePanel(wx.Panel):
def __init__(self, parent, seed):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
self.SetBackgroundColour('cyan')
self.Choicbook = wx.Choicebook(self, wx.ID_ANY)
for i in range(seed):
self.Choicbook.AddPage(ChoicePanelOne(self.Choicbook, seed*2), str(i))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.Choicbook, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class AppFrame(wx.Frame):
""" The main frame of the application
"""
title = 'Application'
WindowSize = (1024, 768)
seed = 2
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title, size=self.WindowSize,
style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
self.create_main_panel()
def create_main_panel(self):
self.panel = TreePanel(self, self.seed)
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = AppFrame()
app.frame.Show()
app.MainLoop()
你应该下载wxPython演示,因为它里面有很多很好的例子。另请参阅wiki或我的blog article。
您可以使用Widget Inspection Tool来帮助您诊断问题,并且它会告诉您父母在哪里以及如何布置sizer等等。