wxPython and loading multiple panels (code included)

Discussion in 'Python' started by flupke, Mar 18, 2005.

  1. flupke

    flupke Guest

    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 ===========================
    flupke, Mar 18, 2005
    #1
    1. Advertising

  2. flupke

    enoola

    Joined:
    Feb 9, 2009
    Messages:
    1
    corrected

    Thanks for this peace of code, very useful.
    Two corrections have been made:
    - we get superposition : to avoid that simply use Hide and Show method of wx.Panel
    - we had an exception : because self.parent was not set in Panel2


    HTML:
    <pre>
    ##-------------  panels.py
    import wx
    
    class PanelOne(wx.Panel):
    	def __init__(self,parent):
    		print """panel One Constructor"""
    		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):
    		print """panel Two Constructor"""
    		wx.Panel.__init__(self, parent, -1)
    		self.parent = parent
    		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)
    </pre>
    
    enoola, Feb 9, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonas
    Replies:
    3
    Views:
    475
    Jonas
    Jan 26, 2005
  2. flupke
    Replies:
    1
    Views:
    574
    Hank Fay
    Jun 25, 2004
  3. Noozer
    Replies:
    7
    Views:
    177
    Mike Brind
    Mar 2, 2006
  4. boczek
    Replies:
    2
    Views:
    146
    boczek
    Jan 4, 2007
  5. jodleren
    Replies:
    1
    Views:
    108
    jodleren
    Sep 18, 2008
Loading...

Share This Page