pythoneventswxpythonwxnotebook

AuiNotebook - Get clicked tab


I am currently using some wxPython's AuiNotebook in one of my projects and I have a problem that can't manage to solve. I think there should be a simple solution but can't manage to find it.

I have created a new class derived from wx.lib.agw.aui.AuiNotebook and I am trying to get the index of a tab at the moment it's clicked. I wrote something like that:

class NewNotebook(wx.lib.agw.aui.AuiNotebook):

    # __init__ function an stuff...

    def OnTabClicked(self, evt):
        index = self.GetSelection()
        print index
        wx.lib.agw.aui.AuiNotebook.OnTabClicked(self, evt)

The aim was to capture the index of the tab clicked on and in some cases, have a special behavior, or just perform a regular click otherwise. However, I think that AuiNotebook.OnTabClicked actually changes the selection among different things. It would explain why index contains the value of the tab that was selected before the click.

I did not a find a way to get the selection of the new tab though. I looked for some information in the captured event but still could not find the one I wanted.

So, does someone know how I can get the selected tab before I call AuiNotebook.OnTabClicked?


Solution

  • There doesn't seem to be a builtin way to do this. The closest I found was something I helped with on the wxPython mailing list, but that had to do with double-clicking.

    Here's one workaround that came to me though. When you first show the frame, set some kind of class property to the currently shown tab (i.e. self.currentTab = 0). Then catch the EVT_AUINOTEBOOK_PAGE_CHANGING or EVT_AUINOTEBOOK_PAGE_CHANGED event and update the property. For me, when I catch EVT_AUINOTEBOOK_PAGE_CHANGING, I always get the index of the tab I'm clicking on, at least on Windows.