Determining which sheet in a wx.Notebook control has focus

Greetings.

I’m a newbie wxPython user and am struggling
a bit with the wx.Notebook control. If someone is able to help steer
me in the right direction, I’d appreciate the help.

As a starting point let’s say I am working
with ZetCode’s skeleton SpreadSheet
code:

http://www.zetcode.com/wxpython/skeletons/

  1. How do I determine which sheet in
    wx.Notebook control currently has focus? And that sheet’s name/label?

As a contrived example, perhaps I have a
dialog with a simple wx.StaticText widget that displays the sheets name
(triggered via a menu selection).

import wx

class
ContrivedDialog(wx.Dialog):

def init(self, parent):

wx.Dialog.init(self, None, -1, title=“Where am I?”)

sizer = wx.BoxSizer(wx.VERTICAL)

whoHasFocus = INSERT_MAGIC_METHOD_HERE # who has focus?

lbl = wx.StaticText(self, -1, "Sheet Name: %s"

% whoHasFocus.GetSOMETHING(), size=(200,25))

btn = wx.Button(self, wx.ID_OK, "Close Me")

sizer.Add(lbl)

sizer.Add(btn)

self.SetSizer(sizer)

sizer.Fit(self)

self.Show()
  1. How do I set the focus to a different
    sheet?

Sticking with my contrived example, here’s a
skeleton.

import
wx

def
init(self, parent):

wx.Dialog.__init__(self, None, -1, title="Where am I?")

sizer = wx.BoxSizer(wx.VERTICAL)

whoHasFocus = INSERT_MAGIC_METHOD1_HERE # who has focus?

newlyFocused = INSERT_MAGIC_METHOD2_HERE #

set focus to different sheet

lbl1 = wx.StaticText(self, -1, "Was on

Sheet Name: %s" % whoHasFocus.GetSOMETHING(), size=(200,25))

lbl2 = wx.StaticText(self, -1,

“Refocused to Sheet Name: %s” % newlyFocused .GetSOMETHING(),
size=(200,25))

btn = wx.Button(self, wx.ID_OK, "Close Me")

sizer.Add(lbl1)

sizer.Add(lbl2)

sizer.Add(btn)

self.SetSizer(sizer)

sizer.Fit(self)

self.Show()

In both examples, the GetSOMETHING() method outputs
the string label used when adding a widget to a sheet in a wx.Notebook
control via:

notebook.AddPage(sheet1,

‘Sheet1’)

So Sheet1 if Sheet1 has focus.

Cheers!

Rob

Greetings.

I'm a newbie wxPython user and am struggling a bit with the wx.Notebook
control. If someone is able to help steer me in the right direction, I'd
appreciate the help.

As a starting point let's say I am working with ZetCode's skeleton
SpreadSheet code:
http://www.zetcode.com/wxpython/skeletons/

1. How do I determine which sheet in wx.Notebook control currently has
focus?

.GetCurrentPage()
Returns the currently selected notebook page or NULL.

And that sheet's name/label?

page = .GetCurrentPage()
page_name = page.GetPageText()

As a contrived example, perhaps I have a dialog with a simple wx.StaticText
widget that displays the sheets name (triggered via a menu selection).

import wx

class ContrivedDialog(wx.Dialog):

def __init__(self, parent):
    wx.Dialog.__init__(self, None, -1, title="Where am I?")
    sizer = wx.BoxSizer(wx.VERTICAL)
    whoHasFocus = INSERT_MAGIC_METHOD_HERE # who has focus?
    lbl = wx.StaticText(self, -1, "Sheet Name: %s" %
whoHasFocus.GetSOMETHING(), size=(200,25))
    btn = wx.Button(self, wx.ID_OK, "Close Me")
    sizer.Add(lbl)
    sizer.Add(btn)
    self.SetSizer(sizer)
    sizer.Fit(self)
    self.Show()

2. How do I set the focus to a different sheet?

You should know the names of each page ("sheet" in this case) because they
had to have been created previously with a name. Let's say you want to set
the focus (the selection) to the page called page2. Then do:

yourNotebook.SetSelection(page2)

You can have a look here for various such Notebook methods:

http://docs.wxwidgets.org/2.6/wx_wxnotebook.html
(this is the wxWidgets docs, sometimes easier to follow)

http://www.wxpython.org/docs/api/wx.Notebook-class.html
(this is the wxPython docs; often one must find methods in parent classes)

···

On Fri, Oct 24, 2008 at 8:59 PM, Open Source User <osuser@minnecoda.com> wrote: