Hi the code below attempts to added a second sub panel if buffer is
selected using the choice control. It sort of works but you need to drag
the frame a bit for the panels to expand to fill it.
1 How can I do this without dragging the frame.
2 Can I create and add sub panels entirely within the event function cos
I've got quite a few or do I have to add them all and hide them, and
just .Show() the ones I want?
Any help much appreciated!
<code>
#!/usr/bin/env python
import wx
import string
class MyDataStruct:
def __init__(self, arg_1):
self.arg_1 = arg_1
my_list = []
# the sizer stuff is not working here but does in dialog.py why?
# took all sizer stuff out and put it back and it worked? compiler seems
a bit flak
# this is for standard stuff
class MySubPanel_1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=-1)
self.SetBackgroundColour("GREEN")
# choice of set type control
st_set_type = wx.StaticText(self, -1, 'set_type ')
set_type_list = ['machine', 'buffer', 'fixed conveyor', 'free
conveyor', 'track_pt', 'vehicle',]
#not safe so stick with unique ids
choice_set_type = wx.Choice(self, 2, (85, 18),
choices=set_type_list)
# this is all the sizers
hbox_set_type = wx.BoxSizer(wx.HORIZONTAL)
hbox_set_type.Add(st_set_type, 0, wx.LEFT, 30)
hbox_set_type.Add(choice_set_type, 0, 50 )
vbox = wx.BoxSizer(wx.VERTICAL) #all controls positioned
in here
vbox.Add(hbox_set_type, 0, wx.TOP, 10)
self.vbox = vbox
# this is for custom stuff
class MySubPanel_2(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=-1)
self.SetBackgroundColour("RED")
# this is all the sizers
hbox_cust_1 = wx.BoxSizer(wx.HORIZONTAL)
hbox_cust_2 = wx.BoxSizer(wx.HORIZONTAL)
st_cust_1 = wx.StaticText(self, -1, 'custom stuff')
hbox_cust_1.Add(st_cust_1, 0, wx.LEFT, 30)
vbox = wx.BoxSizer(wx.VERTICAL) #all controls positioned
in here
vbox.Add(hbox_cust_1, 0, wx.TOP, 10)
#this is a panel that, itself, contains 2 child panels
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=-1)
#create some members
self.parent = parent
self.mydatastruct = MyDataStruct(3)
self.my_ctr = 0
#create sub_panels
panel1 = MySubPanel_1(self) #self passes this object as parent
panel2 = MySubPanel_2(self)
# button to create set
button_create_set = wx.Button(self, 1, 'create set')
self.Bind(wx.EVT_BUTTON, self.create_set, id=1) #how does this
know which button? Is it by id=1 ?
#always stick with a unique id
self.Bind(wx.EVT_CHOICE, self.OnSetTypeChoice, id=2)
sizer = wx.BoxSizer(wx.VERTICAL) #replaced by sizer
sizer.Add(panel1, 1, wx.EXPAND)
sizer.Add(panel2, 1, wx.EXPAND)
sizer.Add(button_create_set, 0, wx.ALIGN_CENTER | wx.TOP |
wx.BOTTOM, 20)
sizer.Hide(panel2, recursive=True)
self.SetSizer(sizer)
self.SetAutoLayout(1)
sizer.Fit(self)
sizer.SetSizeHints(self)
self.panel2 = panel2
self.sizer = sizer
#this adds the custom ctrls dependent upon what type of set you
choose via the set type choice ctrl
def OnSetTypeChoice(self, event):
choice = event.GetString()
if choice == "machine":
self.parent.SetTitle(choice) #test
if choice == "buffer":
#i think i need a frame sizer and that this would do it ie this
needs to be in the frame
self.parent.SetTitle(choice)
self.sizer.Show(self.panel2, recursive=True)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
if choice == "fixed conveyor":
self.parent.SetTitle(choice)
if choice == "free conveyor":
self.parent.SetTitle(choice)
if choice == "track_pt":
self.parent.SetTitle(choice)
if choice == "vehicle":
self.parent.SetTitle(choice)
# event for button that creates set
#this button needs to be at a higher level cos it needs to get stuff
from all panels - ie move it
def create_set(self, event):
if self.mydatastruct.arg_1 == 3:
self.my_ctr = self.my_ctr + 1
obj = MyDataStruct(self.my_ctr)
my_list.append(obj)
my_str = "good"
for x in my_list:
my_str = my_str + str(x.arg_1 * 2) + ","
self.parent.SetTitle(my_str)
else:
self.parent.SetTitle("bad")
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Sizer Demo', size=(400,200))
panel = MyPanel(self)
panel.SetBackgroundColour("BLACK")
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame()
frame.Show(1)
return 1
if __name__ == '__main__':
app = MyApp(0)
app.MainLoop()
</code>