wxPython and loading multiple panels (code included)

F

flupke

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 ===========================
 
Joined
Feb 9, 2009
Messages
1
Reaction score
0
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top