Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > wxPython and loading multiple panels (code included)

Reply
Thread Tools

wxPython and loading multiple panels (code included)

 
 
flupke
Guest
Posts: n/a
 
      03-18-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 ===========================
 
Reply With Quote
 
 
 
 
enoola enoola is offline
Junior Member
Join Date: Feb 2009
Posts: 1
 
      02-09-2009
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 Code:
<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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments