I'm trying to wrap my head around using wx.Simplebook
, and as far as I can tell I've basically copied the python demo, but obviously am missing something vital.
This example should be a wx.Frame
with two wx.Buttons
on the left and the wx.Simplebook
on the right. The two buttons should switch between the pages of the book.
import wx
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,0)
buttonSizer.Add(page2Button,1,0)
book = MainPage.BookTest(self)
pagesSizer.Add(book,1,wx.EXPAND)
self.SetSizer(mainSizer)
self.Layout()
class BookTest (wx.Simplebook):
def __init__(self,parent):
wx.Simplebook.__init__(self,parent)
page1=MainPage.Page1(self)
page2=MainPage.Page2(self)
self.AddPage(page1,"")
self.AddPage(page2,"")
class Page1 (wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self)
sizer = wx.BoxSizer(wx.VERTICAL)
t=wx.StaticText(self,-1,"THIS IS PAGE 1")
sizer.Add(t,1,wx.EXPAND,0)
class Page2 (wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self)
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()
Half the time the build pauses, and nothing is shown (although I get a 'Finished in 2.0 seconds' message), the other half of the time i get error code 3221225477.
if you # the line pagesSizer.Add(book,1,wx.EXPAND)
, the frame opens as planned, but obviously without the simplebook.
Where have I gone wrong?
Thanks,
Sundown
Welcome to StackOverflow.
Your code is missing just a few details. First, the indentation of the classes is wrong. They should all start in the first column. The code suggests (at least to me) that you are thinking about BookTest
, Page1
and Page2
as subclasses of MainPage
. But this is not the correct way to think about it. They are all independent classes and you will use in MainPage
and instance of BookTest
and BookTest
will use an instance of Page1
and an instance of Page2
.
Changing this leads to change the line book = MainPage.BookTest(self)
to book = BookTest(self)
The second problem is that you need to keep track of parent/child relationship for your widgets because this helps a lot to correctly display them. Therefore, you need to pass the parent
argument when starting the wx.Simplebook
and wx.Panel
widget in BookTest
and Page1
and Page2
.
Here is the code,
import wx
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,0)
buttonSizer.Add(page2Button,1,0)
book = BookTest(self)
pagesSizer.Add(book,1,wx.EXPAND)
self.SetSizer(mainSizer)
self.Layout()
class BookTest (wx.Simplebook):
def __init__(self, parent):
wx.Simplebook.__init__(self, parent=parent)
page1=Page1(self)
page2=Page2(self)
self.AddPage(page1,"")
self.AddPage(page2,"")
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)
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()