Can someone please fill in the missing line of code in OnPageChange? I have a notebook control and on a page change event, I want to retrieve the label of the tab that was just selected. GetLabel and GetLabelText both return ''

import wx
import wx.lib.mixins.inspection

class Frame(wx.Frame):

	def __init__(self,parent=None):

		wx.Frame.__init__(self, None, -1, "test notebook",size=(300,300))
		self.Bind(wx.EVT_CLOSE,self.OnExit,self)

		self.CreateStatusBar()

		self.nb = wx.Notebook(self)
		self.nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED,self.OnPageChange)

		for page in ("page 1","page 2","page 3"):

			panel = wx.Panel(self.nb)
			self.nb.AddPage(panel,page)

	def OnPageChange(self,event):

		#current = event.EventObject.?
		self.SetStatusText("current page is ...")

	def OnExit(self,event):

		self.Destroy()

class MyApp(wx.App, wx.lib.mixins.inspection.InspectionMixin):

	def OnInit(self):

		self.Init()  # initialize the inspection tool
		frame = Frame(None)

		frame.Show()
		self.SetTopWindow(frame)

		return True

app = MyApp(redirect=False)
app.MainLoop()

Supplemental question. If, after line 18 I add the line

button = wx.Button(panel,label="Button",size=(60,30))

why, when I run the code, does nothing (no button) show up on the page? Come to think of it, I am not even sure the panels are being displayed. I have a feeling that I am missing something fundamental but I don't know what. As an aside, research done years ago shows that for most office people, their number one goal is to not feel like an idiot while they are working. I am clearly not attaining my goal.

OK. It was painful but I started with a simple notebook that worked - no event handlers, just bare bones, then started adding stuff until it broke. The offending line was

self.nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED,self.OnPageChange)

for some reason, nothing displays in the notebook pages after this line is executed.

If I may rant (politely, of course) for a moment...

I have found, in a programming career spanning 32 years, that there are a number of people out there who are more interested in dazzling you with their intelligence and knowledge than they are in sharing it. Sometimes this is intentional and sometimes not. For example, I spent several hours looking for examples of the notebook control. I even checked the demo program included with wxPython. Instead of simple, straightforward examples I mostly found examples consisting of subclass after subclass and which imported custom modules containing dozens of lines of irrelevant code that did nothing but cloud the explanation. As I tried to explain it to my non-technical wife, if she was looking for an example of how to create a certain stitch pattern, it would be massive overkill to be presented with a 50 page explanation of how to create a complex quilt, even if the explanation contained (somewhere) a very small section showing how to do the stitch pattern in question. So my simple request is that when asked for an example of a particular control, please try to limit the complexity to illustrate only the control in question.

Or am I being unreasonable?

Finally found this little gem

http://wiki.wxpython.org/wxPython%20Platform%20Inconsistencies

To wit...

Symptoms: When switching pages in a wxNotebook control subclass in GTK, everything works fine. When running the same code in MS Windows, the rendering and visibility of widgets is erroneous.

Solution: You are probably catching EVT_NOTEBOOK_PAGE_CHANGED, but in your event handler you aren't passing the event along to the next handler in the chain. You must do so in order to make sure the notebook control stays in a consistent state.

Solution - add the following line of code to the handler

event.Skip()

After much searching and digging (and weeping and gnashing of teeth) I finally have the answer. The following code should illustrate how to create a simple, three-page notebook control with a handler that identifies which page has just been selected. As an aside, I have included the widget inspection tool so that pressing CTRL-ALT-I while the GUI has focus displays the inspector

import wx
import wx.lib.mixins.inspection

class Frame(wx.Frame):

    def __init__(self, *args, **kwargs):

        wx.Frame.__init__(self,*args,**kwargs)
        
        # Create a notebook with three pages. Each page has one button.
        # It is important to give each panel a name because this is the
        # value returned by the expression "tab.GetName()" in the event
        # handler.
        
        nb = wx.Notebook(self)
        nb.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED,self.OnTabChanged)

        for page in ("page 1","page 2","page 3"):
            panel  = wx.Panel(nb,name=page)
            sizer  = wx.FlexGridSizer(cols=13)
            panel.SetSizer(sizer)
            button = wx.Button(panel,label="Button",size=(60,30))
            sizer.Add(button,0)
            nb.AddPage(panel,page)

        self.Show()
        
    def OnTabChanged(self,event):
    
        tab = event.EventObject.GetChildren()[event.Selection]
        print "you selected tab " + tab.GetName()
        
        # If you don't include the following line your display won't
        
        event.Skip()

class MyApp(wx.App, wx.lib.mixins.inspection.InspectionMixin):

    def OnInit(self):

        self.Init()                      # initialize the inspection tool
        frame = Frame(None)
        self.SetTopWindow(frame)

        return True

app = MyApp(redirect=False)
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.