wxPython auinotebook.GetSelection() return index to the first page.

Why do 'GetSelection()' return the index to the first page and not the last created in 'init' and 'new_panel'? It do return correct index in the 'click' method.

The output should be 0 0 1 1 2 2, but mine is 0 0 0 0 0 0.

Running latest version of python and wxpython in ArchLinux. It seems like running this in Windows gives the correct output. Go figure...

Ørjan Pettersen

#!/usr/bin/python

#12_aui_notebook1.py

import wx
import wx.lib.inspection

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)

        self.nb = wx.aui.AuiNotebook(self)

        self.new_panel('Page 1')
        print self.nb.GetSelection()
        self.new_panel('Page 2')
        print self.nb.GetSelection()
        self.new_panel('Page 3')
        print self.nb.GetSelection()

    def new_panel(self, nm):
        pnl = wx.Panel(self)
        pnl.identifierTag = nm
        self.nb.AddPage(pnl, nm)
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.nb, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        pnl.SetFocus() # Have focused the last panel.
        print self.nb.GetSelection()

        pnl.Bind(wx.EVT_LEFT_DOWN, self.click)

    def click(self, event):
        print 'Mouse click'
        print self.nb.GetSelection()
        print self.nb.GetPageText(self.nb.GetSelection())

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, '12_aui_notebook1.py')
        frame.Show()
        self.SetTopWindow(frame)
        return 1

if __name__ == "__main__":
    app = MyApp(0)
# wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

···

pnl.SetFocus
Did you mean: self.nb.SetSelection

Changing the SetFocus line to

self.nb.SetSelection(int(nm[5])-1) # Have focused the last panel.

yields the right results. It’s quite drity workaround for your example, but it’s meant for educational uses only (:

Setting the focus does not mean selecting.

I would like to thank you for showing me how awesome the AUI widgets seem, and…

Arch FTW!

Lucas Boppre Niehues wrote:

pnl.SetFocus

Did you mean: _*/self.nb.SetSelection/*_

Changing the SetFocus line to

self.nb.SetSelection(int(nm[5])-1) # Have focused the last panel.

yields the right results. It's quite drity workaround for your example, but it's meant for educational uses only (:

Setting the focus does not mean selecting.

A not so dirty solution is to use selection=True in addpage()

def new_panel(self, nm):

        pnl = wx.Panel(self)
        pnl.identifierTag = nm
        self.nb.AddPage(pnl, nm, select=True) ### This will trigger a change page event. self.sizer = wx.BoxSizer()
        self.sizer.Add(self.nb, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        #self.nb.SetSelection(self.nb.GetPageCount()-1) ### This is also a solution.
        pnl.SetFocus() # Have focused the last panel.
        print self.nb.GetSelection()

I would like to thank you for showing me how awesome the AUI widgets seem, and...

Arch FTW!

No problem. :slight_smile: Arch, yes indeed.