wxPython and loading multiple panels (code included)

flupke flupke at nonexistingdomain.com
Fri Mar 18 11:05:41 EST 2005


Hi,

i need to develop a gui which will load several "windows" depending on 
what the users selects in the menu and i thought i could accomplish this 
with panels.
What i'm trying to do to test this is load an initial panel and then 
when the user hits a button load a second panel. This doesn't seem to work.

1. what's the appropriate way of switching panels?
2. the loadNewPanel function seem wrong. I always get loading panel two".

Thanks,
Benedict

==================== multi.py ===========================
import wx
from multi_panel import *

active_frame = 1

class MyFrame(wx.Frame):
     def __init__(self, *args, **kwds):
         kwds["style"] = wx.DEFAULT_FRAME_STYLE
         wx.Frame.__init__(self, *args, **kwds)
         self.panel = PanelOne(self)
         self.__set_properties()
         self.__do_layout()

     def __set_properties(self):
         self.SetTitle("frame_1")

     def __do_layout(self):
         self.sizer_1 = wx.BoxSizer(wx.VERTICAL)
         self.sizer_1.Add(self.panel, 1, wx.EXPAND, 0)
         self.SetAutoLayout(True)
         self.SetSizer(self.sizer_1)
         self.sizer_1.Fit(self)
         self.sizer_1.SetSizeHints(self)
         self.Layout()

     def loadNewPanel(self,invoker):
         if isinstance(invoker,PanelOne):
             print "loading panel two"
             self.panel = PanelTwo(self)
         else:
             print "loading panel one"
             self.panel = PanelOne(self)
         self.sizer_1.Fit(self)
         self.sizer_1.SetSizeHints(self)

class MyApp(wx.App):
     def OnInit(self):
         wx.InitAllImageHandlers()
         frame = MyFrame(None, -1, "This is a 
wx.Frame",pos=(0,0),size=(640,480),style = wx.DEFAULT_FRAME_STYLE)
         self.SetTopWindow(frame)
         frame.Show()
         return 1

if __name__ == "__main__":
     app = MyApp(0)
     app.MainLoop()
==================== multi.py ===========================

and

==================== panels.py ===========================
import wx

class PanelOne(wx.Panel):
     def __init__(self,parent):
         wx.Panel.__init__(self, parent, -1)
         self.parent = parent
         self.label = wx.StaticText(self, -1, "panel one")
         self.switch_panel = wx.Button(self, -1, "Switch to panel two", 
(50,50))
         self.Bind(wx.EVT_BUTTON, self.__OnButton, self.switch_panel)
         self.__do_layout()

     def __do_layout(self):
         self.grid_sizer = wx.GridSizer(5, 5, 4, 4)
         self.grid_sizer.Add(self.label, 0, wx.FIXED_MINSIZE, 0)
         self.grid_sizer.Add(self.switch_panel, 0, wx.FIXED_MINSIZE, 0)

         self.SetAutoLayout(True)
         self.SetSizer(self.grid_sizer)

         self.grid_sizer.Fit(self)
         self.grid_sizer.SetSizeHints(self)

     def __OnButton(self,event):
         print "OnButton"
         self.parent.loadNewPanel(self)

class PanelTwo(wx.Panel):
     def __init__(self,parent):
         wx.Panel.__init__(self, parent, -1)
         self.label = wx.StaticText(self, -1, "panel two")
         self.switch_panel = wx.Button(self, -1, "Switch to panel one", 
(50,50))
         self.Bind(wx.EVT_BUTTON, self.__OnButton, self.switch_panel)
         self.__do_layout()

     def __do_layout(self):
         self.grid_sizer = wx.GridSizer(5, 5, 4, 4)
         self.grid_sizer.Add(self.label, 0, wx.FIXED_MINSIZE, 0)
         self.grid_sizer.Add(self.switch_panel, 0, wx.FIXED_MINSIZE, 0)

         self.SetAutoLayout(True)
         self.SetSizer(self.grid_sizer)

         self.grid_sizer.Fit(self)
         self.grid_sizer.SetSizeHints(self)

     def __OnButton(self,event):
         print "OnButton"
         self.parent.loadNewPanel(self)
==================== panels.py ===========================



More information about the Python-list mailing list