Skip to content

Instantly share code, notes, and snippets.

@joranbeasley
Created October 29, 2014 17:48
Show Gist options
  • Save joranbeasley/37becd81ff2285fcc933 to your computer and use it in GitHub Desktop.
Save joranbeasley/37becd81ff2285fcc933 to your computer and use it in GitHub Desktop.
import wx
def set_widget_value(widget,value):
if hasattr(widget,"SetWidgetValue"):
return widget.SetWidgetValue(value)
if isinstance(widget,wx.Choice):
return widget.SetStringSelection(value)
if hasattr(widget,"SetValue"):
return widget.SetValue(value)
if hasattr(widget,"SetLabel"):
return widget.SetLabel(value)
else:
raise Exception("Unknown Widget Type : %r"%widget)
def get_widget_value(widget):
if hasattr(widget,"GetWidgetValue"):
return widget.GetWidgetValue()
if isinstance(widget,wx.Choice):
return widget.GetStringSelection()
if hasattr(widget,"GetValue"):
return widget.GetValue()
if hasattr(widget,"GetLabel"):
return widget.GetLabel()
else:
raise Exception("Unknown Widget Type : %r"%widget)
class WidgetManager(wx.Panel):
def __init__(self,parent):
self._parent = parent
wx.Panel.__init__(self,parent,-1)
self.CreateWidgets()
def CreateWidgets(self):
#create all your widgets here
self.widgets = {}
def SetWidgetValue(self,value):
if isinstance(value,dict):
for k,v in value.items():
set_widget_value(self.widgets.get(k),v)
else:
raise Exception("Expected a dictionary but got %r"%value)
def GetWidgetValue(self):
return dict([(k,get_widget_value(v))for k,v in self.widgets])
class UserPanel(WidgetManager):
def __init__(self,p):
WidgetManager.__init__(self,p)
self.SetSizer(self.DoLayout())
self.Layout()
self.Fit()
def DoLayout(self):
sz = wx.BoxSizer()
sz.Add(self.widgets["username"],0,wx.ALL,25)
return sz
def CreateWidgets(self):
#create all your widgets here
self.widgets = {
"username":wx.TextCtrl(self,-1,pos=(10,10))
}
class MainPanel(UserPanel):
def DoLayout(self):
sz = wx.BoxSizer()
sz.Add(self.widgets["userpanel"],0,wx.ALL,10)
def CreateWidgets(self):
#create all your widgets here
self.widgets = {
"userpanel":UserPanel(self)
}
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,"Test Frame!!!")
self.main = MainPanel(self)
self.Show()
a = wx.App(redirect=0)
mf = MainFrame()
mf.main.SetWidgetValue({"userpanel":{"username":"bob"}})
a.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment