0

I'm a newbie in wxpython and have been trying to put some widgets within books(treebook, notebook, choicebook) I have often ended up with some widgets placed inside containers not responding to events. I'm not sure what I'm doing wrong. Below is one of my code

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

In this example. The choice book and list does seem to be working. What is that I'm doing wrong ?

2
  • Is this all of your code? You never call the Bind() function, nor do you specify any handlers. What events are you trying to catch? What do you want your program to do when they catch them? What does "not working" mean? Please explain your problem in detail including what you wish to accomplish, what your current behaviour is, and what you've tried. Commented Jul 12, 2012 at 10:38
  • Hey, What I was trying to mean was that when user tries to change the combo box of the innermost ChoiceBook it does not toggle. When user tries to scroll the list the scrollbar does not move. I'm not trying to catch any events, but would like to switch the choicebook pages and scroll. Which doesn't seem to be happening. Commented Jul 12, 2012 at 11:20

1 Answer 1

2

It's a parenting issue. You are setting all the ChoicePanelOne instance's parents as the TreePanel when it should be the ChoicBook. And you're doing the same thing in ChoicePanelOne where you create a whole bunch of ChoicePanelTwo's whose parents are ChoicePanelOne when they should be RegisterList. Check out the slightly changed code below:

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

You should download the wxPython demo as it has lots of good examples in it. Also see the wiki or my blog article.

You could use the Widget Inspection Tool to help you diagnose the problem as well as it will show you what parent is where and how your sizers are laid out, among other things.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, That was helpful. I will have a look at the example, wiki and the blog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.