1

I am trying get an event to fire when the user selects or changes a tab on a notebook. Here is the code I have. In the PageOne class, I have tried to bind the wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED with ChangingTest(self, evt) function. It seems like no matter what event I try to use when binding, I cannot get an event to fire. Is this possible? What am I missing?

I have no problem clicking the button and calling self.ChangingTest, but I want to click on the tab to call self.ChangingTest.

import wx
import wx.lib.inspection

class PageOne(wx.Panel):
   def __init__(self, parent):
       wx.Panel.__init__(self, parent)
       box = wx.BoxSizer(wx.VERTICAL)
       # Want the users' click on the panel tab to fire an event and 
       # call ChangingTest
       self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.ChangingTest)

       cmdClick = wx.Button(self, wx.ID_CLOSE, "Click Me")
       cmdClick.Bind(wx.EVT_BUTTON, self.ChangingTest)
       box.Add(cmdClick, 0, wx.ALL, 10)


   def ChangingTest(self, evt):
       print "ChangingTest2"


class PageTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageTwo object", (40,40))

class PageThree(wx.Panel):
   def __init__(self, parent):
       wx.Panel.__init__(self, parent)
       t = wx.StaticText(self, -1, "This is a PageThree object", (60,60))


class MainFrame(wx.Frame):
   def __init__(self):
       wx.Frame.__init__(self, None, title="Simple Notebook Example")

       # Here we create a panel and a notebook on the panel
       p = wx.Panel(self)
       nb = wx.Notebook(p)

       # create the page windows as children of the notebook
       page1 = PageOne(nb)
       page2 = PageTwo(nb)
       page3 = PageThree(nb)

       # add the pages to the notebook with the label to show on the tab
       nb.AddPage(page1, "Page 1")
       nb.AddPage(page2, "Page 2")
       nb.AddPage(page3, "Page 3")

       # finally, put the notebook in a sizer for the panel to manage
       # the layout
       sizer = wx.BoxSizer()
       sizer.Add(nb, 1, wx.EXPAND)
       p.SetSizer(sizer)



if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

I have the working version here:

import wx

class PageOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageOne object", (20,20))

class PageTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageTwo object", (40,40))

class PageThree(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageThree object", (60,60))


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Simple Notebook Example")

        # Here we create a panel and a notebook on the panel
        p = wx.Panel(self)
        nb = wx.Notebook(p)

        # create the page windows as children of the notebook
        page1 = PageOne(nb)
        page2 = PageTwo(nb)
        page3 = PageThree(nb)

        # add the pages to the notebook with the label to show on the tab
        nb.AddPage(page1, "Page 1")
        nb.AddPage(page2, "Page 2")
        nb.AddPage(page3, "Page 3")

        # finally, put the notebook in a sizer for the panel to manage
        # the layout
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

    # bind event to notebook
        nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.ChangingTest)

    def ChangingTest(self, evt):
       print "It worked!"


if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

1 Answers1

3

Use wx.EVT_NOTEBOOK_PAGE_CHANGED instead of wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED because you are using wx.Notebook, not wx.aui.AuiNotebook. Also, you should bind not the PageOne class but the the notebook object. So you could write parent.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.ChangingTest) or you could bind it outside the PageOne class (nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, ...)).

Anna
  • 373
  • 1
  • 8
  • Thanks for the information. I was binding the event to the wrong object. Once I did the "bind" on the notebook and not PageOne, it worked fine. Thanks again for your help. – Clete Boyce Mar 15 '13 at 18:24
  • Here is the working version of the code. class MainFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, title="Simple Notebook Example") # Here we create a panel and a notebook on the panel p = wx.Panel(self) nb = wx.Notebook(p) # create the page windows as children of the notebook page1 = PageOne(nb) ... # bind event to notebook nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.ChangingTest) def ChangingTest(self, evt): print "It worked!" – Clete Boyce Mar 15 '13 at 18:56