Sundown
Sundown

Reputation: 27

How do I bind the page selection of a wx.simplebook to external buttons?

I'm trying to bind the .ChangeSelection() function of a simplebook to buttons that are not in the book, but in the same frame.

The example code in its current form is a simply set out frame with buttons on the left for the pages and the book itself shown on the right. at the moment the LHS buttons are not bound and the RHS wx.simplebook is showing a blank page '0'.

I'd like to have the buttons show the respective page on pressing, but am struggling to find suitable example simplebooks online to learn from that are in this format.

import wx
### SIMPLEBOOK TEST
class MainPage (wx.Frame):
    def __init__ (self,parent):
        wx.Frame.__init__ ( self, None, 1, title = "NOTEBOOK", pos= wx.DefaultPosition, size = wx.Size( 320,400 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
        mainSizer = wx.BoxSizer(wx.HORIZONTAL)
        buttonSizer = wx.BoxSizer(wx.VERTICAL)
        pagesSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(buttonSizer,1,wx.EXPAND,0)
        mainSizer.Add(pagesSizer,1,wx.EXPAND,0)
        page1Button = wx.Button(self,wx.ID_ANY,"Page 1",wx.DefaultPosition,wx.DefaultSize,0)
        page2Button = wx.Button(self,wx.ID_ANY,"Page 2",wx.DefaultPosition,wx.DefaultSize,0)
        buttonSizer.Add(page1Button,1,wx.ALL|wx.EXPAND,0)
        buttonSizer.Add(page2Button,1,wx.ALL|wx.EXPAND,0)
        book = BookTest(self)
        pagesSizer.Add(book,1,wx.EXPAND|wx.ALL,2)
        self.SetSizer(mainSizer)
        self.Layout()
        ###########################################################
        # My attempts to bind the page selection to the buttons.
        #page1Button.Bind(wx.EVT_BUTTON,BookTest.page1sel())
        #page2Button.Bind(wx.EVT_BUTTON,page2sel())
        ###########################################################

class BookTest (wx.Simplebook):
    def __init__(self, parent):
        wx.Simplebook.__init__(self, parent=parent)
        blankpage=Blankpage(self)
        page1=Page1(self)
        page2=Page2(self)
        self.AddPage(blankpage,"")
        self.AddPage(page1,"")
        self.AddPage(page2,"")

    #### Here are the attempts to change page, both in a nested def and standalone.
    def page1sel():
       self.ChangeSelection(1)

def page2sel():
    BookTest.ChangeSelection(2)

class Blankpage (wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self,parent=parent)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.Show()

class Page1 (wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self,parent=parent)
        sizer = wx.BoxSizer(wx.VERTICAL)
        t=wx.StaticText(self,-1,"THIS IS PAGE 1")
        sizer.Add(t,1,wx.EXPAND,0)
        self.Show()

class Page2 (wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self, parent=parent)
        sizer = wx.BoxSizer(wx.VERTICAL)
        t=wx.StaticText(self,-1,"THIS IS PAGE 2")
        sizer.Add(t,1,wx.EXPAND,0)

if __name__ == '__main__':
    app = wx.App()
    frm = MainPage(None)
    frm.Centre()
    frm.Show()
    app.MainLoop()

Thanks, Sundown

Upvotes: 1

Views: 161

Answers (1)

kbr85
kbr85

Reputation: 1436

You were really close. Only two details were missing.

1- You call the method page1sel from class BookTest through the instance of the class that you create in the class MainPage. This means binding like this:

page1Button.Bind(wx.EVT_BUTTON,book.page1sel)

2- The method page1sel needs to receive the event from the Bind. So it must be defined as:

def page1sel(self, event):

The same goes for page2sel that (in my opinion) should be a method of class BookTest.

Code:

import wx
### SIMPLEBOOK TEST
class MainPage (wx.Frame):
    def __init__ (self,parent):
        wx.Frame.__init__ ( self, None, 1, title = "NOTEBOOK", pos= wx.DefaultPosition, size = wx.Size( 320,400 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
        mainSizer = wx.BoxSizer(wx.HORIZONTAL)
        buttonSizer = wx.BoxSizer(wx.VERTICAL)
        pagesSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(buttonSizer,1,wx.EXPAND,0)
        mainSizer.Add(pagesSizer,1,wx.EXPAND,0)
        page1Button = wx.Button(self,wx.ID_ANY,"Page 1",wx.DefaultPosition,wx.DefaultSize,0)
        page2Button = wx.Button(self,wx.ID_ANY,"Page 2",wx.DefaultPosition,wx.DefaultSize,0)
        buttonSizer.Add(page1Button,1,wx.ALL|wx.EXPAND,0)
        buttonSizer.Add(page2Button,1,wx.ALL|wx.EXPAND,0)
        book = BookTest(self)
        pagesSizer.Add(book,1,wx.EXPAND|wx.ALL,2)
        self.SetSizer(mainSizer)
        self.Layout()
        ###########################################################
        # My attempts to bind the page selection to the buttons.
        page1Button.Bind(wx.EVT_BUTTON,book.page1sel)
        page2Button.Bind(wx.EVT_BUTTON,book.page2sel)
        ###########################################################

class BookTest (wx.Simplebook):
    def __init__(self, parent):
        wx.Simplebook.__init__(self, parent=parent)
        blankpage=Blankpage(self)
        page1=Page1(self)
        page2=Page2(self)
        self.AddPage(blankpage,"")
        self.AddPage(page1,"")
        self.AddPage(page2,"")

    #### Here are the attempts to change page, both in a nested def and standalone.
    def page1sel(self, event):
       self.ChangeSelection(1)

    def page2sel(self, event):
        self.ChangeSelection(2)

class Blankpage (wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self,parent=parent)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.Show()

class Page1 (wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self,parent=parent)
        sizer = wx.BoxSizer(wx.VERTICAL)
        t=wx.StaticText(self,-1,"THIS IS PAGE 1")
        sizer.Add(t,1,wx.EXPAND,0)
        self.Show()

class Page2 (wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self, parent=parent)
        sizer = wx.BoxSizer(wx.VERTICAL)
        t=wx.StaticText(self,-1,"THIS IS PAGE 2")
        sizer.Add(t,1,wx.EXPAND,0)

if __name__ == '__main__':
    app = wx.App()
    frm = MainPage(None)
    frm.Centre()
    frm.Show()
    app.MainLoop()

Upvotes: 1

Related Questions