The following code has a single frame with a main panel and 2 sub
panels.
Sub panel 'panel1' gets added up front and sub panel 'panel2' gets added
when you select buffer in the 1st choice control.
Problems
1 Now that panel1 has controls added, its not opening sufficiently to
show all of the controls.
2 When you select the buffer option in the 1st choice control, panel2
overwrites some of panel1.
How can I address these. Is there some kind of skeleton that I can
adhere to for nested panels so that everything I add gets shown.
Any help much appreciated.
<code>
#!/usr/bin/env python
#bug 1st subpanel 'panel1' is not opening sufficiently to show all
controls
import wx
import string
class MyDataStruct:
def __init__(self, arg_1):
self.arg_1 = arg_1
my_list =
#these are globals but i'm using them as variables (could use a list and
return position)
id_button_create_set = 1
id_st_set_type = id_button_create_set + 1
class MySubPanel_1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=-1)
self.SetBackgroundColour("GREEN")
# labels for controls
st_set_type = wx.StaticText(self, -1, 'set_type ')
st_name = wx.StaticText(self, -1, 'name: ')
st_quantity = wx.StaticText(self, -1, 'quantity:')
st_capacity = wx.StaticText(self, -1, 'capacity: ')
st_x = wx.StaticText(self, -1, 'x: ')
st_y = wx.StaticText(self, -1, 'y: ')
st_cnt_q = wx.StaticText(self, -1, 'cnt_q: ')
st_per_line = wx.StaticText(self, -1, 'per_line:')
st_x_inc = wx.StaticText(self, -1, 'x_inc: ')
st_y_inc = wx.StaticText(self, -1, 'y_inc: ')
# controls themselves
set_type_list = ['machine', 'buffer', 'fixed conveyor', 'free
conveyor', 'track_pt', 'vehicle',]
choice_set_type = wx.Choice(self, id_st_set_type, (85, 18),
choices=set_type_list)
tc_name = wx.TextCtrl(self, -1, size=(180, -1))
tc_quantity = wx.TextCtrl(self, -1, size=(180, -1))
tc_capacity = wx.TextCtrl(self, -1, size=(180, -1))
tc_x = wx.TextCtrl(self, -1, size=(180, -1),
value="XXX")
tc_y = wx.TextCtrl(self, -1, size=(180, -1))
set_type_list = ['count', 'q left', 'q right', 'q up', 'q
down']
choice_cnt_q = wx.Choice(self, -1, (85, 18),
choices=set_type_list)
tc_per_line = wx.TextCtrl(self, -1, size=(180, -1))
tc_x_inc = wx.TextCtrl(self, -1, size=(180, -1))
tc_y_inc = wx.TextCtrl(self, -1, size=(180, -1))
# control default values
tc_y_inc.SetValue("blob") #set default value
# make horizontal boxes to slot into vertical box vbox
vbox = wx.BoxSizer(wx.VERTICAL)
hbox_set_type = wx.BoxSizer(wx.HORIZONTAL)
hbox_name = wx.BoxSizer(wx.HORIZONTAL)
hbox_quantity = wx.BoxSizer(wx.HORIZONTAL)
hbox_capacity = wx.BoxSizer(wx.HORIZONTAL)
hbox_x = wx.BoxSizer(wx.HORIZONTAL)
hbox_y = wx.BoxSizer(wx.HORIZONTAL)
hbox_cnt_q = wx.BoxSizer(wx.HORIZONTAL)
hbox_per_line = wx.BoxSizer(wx.HORIZONTAL)
hbox_x_inc = wx.BoxSizer(wx.HORIZONTAL)
hbox_y_inc = wx.BoxSizer(wx.HORIZONTAL)
# add label to horizontal boxes
hbox_set_type.Add (st_set_type, 0, wx.LEFT, 10)
hbox_name.Add (st_name, 0, wx.LEFT, 10)
hbox_quantity.Add (st_quantity, 0, wx.LEFT, 10)
hbox_capacity.Add (st_capacity, 0, wx.LEFT, 10)
hbox_x.Add (st_x, 0, wx.LEFT, 10)
hbox_y.Add (st_y, 0, wx.LEFT, 10)
hbox_cnt_q.Add (st_cnt_q, 0, wx.LEFT, 10)
hbox_per_line.Add (st_per_line, 0, wx.LEFT, 10)
hbox_x_inc.Add (st_x_inc, 0, wx.LEFT, 10)
hbox_y_inc.Add (st_y_inc, 0, wx.LEFT, 10)
# add controls to horizontal boxes
hbox_set_type.Add (choice_set_type, 10)
hbox_name.Add (tc_name, 0, wx.LEFT, 20)
hbox_quantity.Add (tc_quantity, 0, wx.LEFT, 35)
hbox_capacity.Add (tc_capacity, 0)
hbox_x.Add (tc_x, 0)
hbox_y.Add (tc_y, 0)
hbox_cnt_q.Add (choice_cnt_q, 0)
hbox_per_line.Add (tc_per_line, 0)
hbox_x_inc.Add (tc_x_inc, 0)
hbox_y_inc.Add (tc_y_inc, 0)
# create & add horizontal boxes to vertical box
vbox.Add(hbox_set_type, 0, wx.TOP, 10) #added for listbox
vbox.Add(hbox_name, 0, wx.TOP, 10)
vbox.Add(hbox_quantity, 0, wx.TOP, 10)
vbox.Add(hbox_capacity, 0, wx.TOP, 10)
vbox.Add(hbox_x, 0, wx.TOP, 10)
vbox.Add(hbox_y, 0, wx.TOP, 10)
vbox.Add(hbox_cnt_q, 0, wx.TOP, 10) #added for listbox
vbox.Add(hbox_per_line, 0, wx.TOP, 10)
vbox.Add(hbox_x_inc, 0, wx.TOP, 10)
vbox.Add(hbox_y_inc, 0, wx.TOP, 10)
self.SetSizer(vbox)
self.Layout()
# vbox.Fit(self) # this is for custom stuff
# vbox.SetSizeHints(self)
# self.Layout()
self.vbox = vbox
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 contains 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
#always stick with a unique id
self.Bind(wx.EVT_CHOICE, self.OnSetTypeChoice,id=id_st_set_type)
sizer = wx.BoxSizer(wx.VERTICAL) #replaced by sizer
sizer.Add(panel1, 1, wx.EXPAND)
self.SetSizer(sizer)
self.Layout()
#self.SetAutoLayout(1)
#sizer.Fit(self)
#sizer.SetSizeHints(self)
self.sizer = sizer
# button to create set
def add_button_create_set(self): #you MUST put self as arg here to
reference self but must not specify self in call. inconsistent!
self.button_create_set = wx.Button(self, id_button_create_set,
'create set')
self.Bind(wx.EVT_BUTTON, self.create_set, id=id_button_create_set)
self.sizer.Add(self.button_create_set, 0, wx.ALIGN_CENTER | wx.TOP
wx.BOTTOM, 20)
#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.panel2 = MySubPanel_2(self)
self.sizer.Add(self.panel2, 1, wx.EXPAND)
#self.sizer.Show(self.panel2, recursive=True)
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)
self.add_button_create_set()
self.SetSizer(self.sizer)
self.Layout() #from robin@alldunn.com
wxPython-users@lists.wxwidgets.org in response to my question
#self.parent.SendSizeEvent()
# event for button that creates set
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>