0

我是 wxpython 的新手,一直在尝试将一些小部件放入书籍(treebook、notebook、choicebook)中,我经常将一些小部件放置在不响应事件的容器中。我不确定我做错了什么。下面是我的代码之一

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()

在这个例子中。选择书和清单似乎确实有效。我做错了什么?

4

1 回答 1

2

这是育儿问题。当它应该是 ChoicBook 时,您将所有 ChoicePanelOne 实例的父级设置为 TreePanel。你在 ChoicePanelOne 中做同样的事情,你创建了一大堆 ChoicePanelTwo,它们的父级是 ChoicePanelOne,而它们应该是 RegisterList。查看下面稍微更改的代码:

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或我的博客文章

您可以使用小部件检查工具来帮助您诊断问题,并且它会向您显示父级在哪里以及您的尺寸器是如何布置的,等等。

于 2012-07-12T14:15:58.737 回答