trouble resizing on the fly panels

It works with ON_THE_FLY = 0 but not ON_THE_FLY = 1
I'm only changing two lines or more correctly, when those 2 lines get
executed. This makes a lot of difference - 'sizer wise'

Any help much appreciated

"""
setting ON_THE_FLY = 0
   creates all panels at the outset and places Subpanel_2 under
Subpanel_1 in a vertical box.
   Great!

setting ON_THE_FLY = 1
   creates Subpanel_2 in response to the 'machine' menu item being
clicked.
   I was hoping that Subpanel_2 (ie the red bit) would be positioned as
above.
   It doesn't and I'd like to know what I can do about it

The difference between the two is simply that I've replaced 2 lines in
class Panel
with 2 lines in class MyFrame

Any help much appreciated.
"""

<code>
#!/usr/bin/env python

import wx
import string

ON_THE_FLY = 1

id_choice_cnt_q = 1
id_lb_all_sets = id_choice_cnt_q + 1

class Subpanel_1(wx.Panel):
   def __init__(self, parent,x,y): #x,y coords of mouse posn passed
from padXX module
      wx.Panel.__init__(self, parent,id=-1)
      self.SetBackgroundColour("GREEN")

      lbl1 = wx.StaticText(self, -1, 'name:')
      lbl2 = wx.StaticText(self, -1, 'quantity:')
      lbl3 = wx.StaticText(self, -1, 'capacity:')
      lbl4 = wx.StaticText(self, -1, 'x:')
      lbl5 = wx.StaticText(self, -1, 'y:')
      lbl6 = wx.StaticText(self, -1, 'cnt_q:')
      lbl7 = wx.StaticText(self, -1, 'per_line:')
      lbl8 = wx.StaticText(self, -1, 'x_inc:')
      lbl9 = wx.StaticText(self, -1, 'y_inc:')
  
      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=str(x))
      tc_y = wx.TextCtrl(self, -1, size=(180, -1),
value=str(y))
      cnt_q_list = ['count', 'q left', 'q right', 'q up', 'q down']
      choice_cnt_q = wx.Choice(self, -1, (85, 18),
choices=cnt_q_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))
      
      sz_1 = wx.FlexGridSizer(rows=2, cols=2, hgap=10, vgap=5)
      sz_1.AddMany([ lbl1,tc_name,
                        lbl2,tc_quantity,
                        lbl3,tc_capacity,
                        lbl4,tc_x,
                        lbl5,tc_y,
                        lbl6,choice_cnt_q,
                        lbl7,tc_per_line,
                        lbl8,tc_x_inc,
                        lbl9,tc_y_inc
                     ])

      self.SetSizer(sz_1)
      self.sz_1 = sz_1

class Subpanel_2(wx.Panel):
   def __init__(self, parent):
      wx.Panel.__init__(self, parent, id=-1)
      self.SetBackgroundColour("RED")
      lbl = wx.StaticText(self, -1, 'custom stuff')

      sz_2 = wx.BoxSizer(wx.HORIZONTAL)
      sz_2.Add (lbl, 0, wx.LEFT, 10)
      
      sz_1 = wx.BoxSizer(wx.VERTICAL)
      sz_1.Add(sz_2, 0, wx.TOP, 10) #added for listbox
      self.SetSizer(sz_1)
      self.sz_1 = sz_1
        
#this is a panel that contains child panels
class Panel(wx.Panel):
   def __init__(self, parent,x,y):
      wx.Panel.__init__(self, parent, id=-1)
      self.SetBackgroundColour("BLUE")
      self.parent = parent

      subpanel_1 = Subpanel_1(self,x,y)

···

#===============================
      if ON_THE_FLY == 0:
         subpanel_2 = Subpanel_2(self)
      #===============================

      lb_txt =
['blibby','blobby','blabby','blubby','a','b','c','d','e','f','g','a','b','c','d','e','f','g']
      lb_all_sets =
wx.ListBox(choices=lb_txt,id=id_lb_all_sets,name='listBox1',parent=self,style=0)
      lb_all_sets.SetBackgroundColour( "YELLOW" )
      lb_all_sets.Bind( wx.EVT_LISTBOX,
self.on_lb_all_sets,id=id_lb_all_sets )

      sz_2_1 = wx.BoxSizer(wx.VERTICAL)
      sz_2_1.Add(subpanel_1, 0, wx.EXPAND)

      #=====================================
      if ON_THE_FLY == 0:
         sz_2_1.Add(subpanel_2, 0, wx.EXPAND)
      #=====================================

      #=================================================
      # this is what I put in MyFrame, just here so can compare with the
above two lines
      #if ON_THE_FLY == 1:
      # self.panel.subpanel_2 = Subpanel_2(self.panel)
      # self.panel.sz_2_1.Add(self.panel.subpanel_2, 0, wx.EXPAND)
      #=================================================

      sz_2_2 = wx.BoxSizer(wx.VERTICAL)
      sz_2_2.Add(lb_all_sets,0,wx.EXPAND)

      sz_1 = wx.BoxSizer(wx.HORIZONTAL)
      sz_1.Add(sz_2_1)
      sz_1.Add(sz_2_2)

      self.SetSizer(sz_1)
      self.sz_1 = sz_1

   def on_lb_all_sets(self, event):
      self.list_index_selected = event.GetSelection()
      self.parent.SetTitle( "in list box function: ")

class MyFrame(wx.Frame):
   def __init__(self,x,y):
      #wx.Frame.__init__(self, None, -1, 'Sizer Demo', size=(1000,900) )
      wx.Frame.__init__(self, None, -1, 'Sizer Demo' ) #new way of doing
it relying on FitDynamically() to 'autosize'

      panel = Panel(self,x,y)
      #panel.SetBackgroundColour("BLACK")

      #=======================================================
      #menu stuff
      menu = wx.Menu()
      mi_machine = menu.Append(-1, "machine") #mi
stands for 'menu item'
      mi_buffer = menu.Append(-1, "buffer")
      mi_fixed_conveyor = menu.Append(-1, "fixed_conveyor")
      mi_free_conveyor = menu.Append(-1, "free_conveyor")
      mi_track_pt = menu.Append(-1, "track_pt")
      mi_vehicle = menu.Append(-1, "vehicle")
      menu.AppendSeparator()
      exit = menu.Append(-1, "Exit")
      self.Bind(wx.EVT_MENU, self.On_mi_machine, mi_machine)
      self.Bind(wx.EVT_MENU, self.OnExit, exit)
      menuBar = wx.MenuBar()
      menuBar.Append(menu, "Simple Menu")
      self.SetMenuBar(menuBar)
      #=======================================================
      
      #sizer stuff
      sz_1 = wx.BoxSizer(wx.VERTICAL) #replaced by sizer
      sz_1.Add(panel, 1, wx.EXPAND)
      self.SetSizer(sz_1)
      self.FitDynamically()

      self.panel = panel
   
   #menu stuff
   def On_mi_machine(self, event):
      self.SetTitle( "machine")
      wx.MessageBox("You selected item_machine")
      
      #=================================================
      if ON_THE_FLY == 1:
         self.panel.subpanel_2 = Subpanel_2(self.panel)
         self.panel.sz_2_1.Add(self.panel.subpanel_2, 0, wx.EXPAND)
      #=================================================

   #menu stuff
   def OnExit(self, event):
      self.Close()
   
   def FitDynamically(self): #given to me by stani from my forum
question
      self.SetMinSize(self.GetSizer().GetMinSize())
      self.Fit()

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(3,4) #3,4 simulates the mouse's x,y coords
passed to this module (ie frame()) by pad_XXX.py
        frame.Show(1)
        return 1

if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()

</code>

me wrote:

It works with ON_THE_FLY = 0 but not ON_THE_FLY = 1
I'm only changing two lines or more correctly, when those 2 lines get
executed. This makes a lot of difference - 'sizer wise'

Any help much appreciated

"""
setting ON_THE_FLY = 0 creates all panels at the outset and places Subpanel_2 under
Subpanel_1 in a vertical box.
   Great!

setting ON_THE_FLY = 1 creates Subpanel_2 in response to the 'machine' menu item being
clicked. I was hoping that Subpanel_2 (ie the red bit) would be positioned as
above.
   It doesn't and I'd like to know what I can do about it

The difference between the two is simply that I've replaced 2 lines in
class Panel
with 2 lines in class MyFrame

Normal sizer automatic layout happens when the window owning the sizer gets a size event. Since in your ON_THE_FLY case the window is already at its final size there is not another size event after you add the 2nd panel, so there is no automatic layout. That doesn't mean that you can't do it yourself however, just add a call to self.panel.Layout.

BTW, when sending sample code with long lines please send it as an attachment, not a copy and paste. I don't have time to undo word-wrap just to run a sample.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin
self.Fit() did the trick!
Thanks for your explanation and sorry for your inconvenience!
Will include code as attachment next time.
Rgds

···

On Wed, 2007-06-20 at 15:54 -0700, Robin Dunn wrote:

me wrote:
> It works with ON_THE_FLY = 0 but not ON_THE_FLY = 1
> I'm only changing two lines or more correctly, when those 2 lines get
> executed. This makes a lot of difference - 'sizer wise'
>
> Any help much appreciated
>
>
> """
> setting ON_THE_FLY = 0
> creates all panels at the outset and places Subpanel_2 under
> Subpanel_1 in a vertical box.
> Great!
>
> setting ON_THE_FLY = 1
> creates Subpanel_2 in response to the 'machine' menu item being
> clicked.
> I was hoping that Subpanel_2 (ie the red bit) would be positioned as
> above.
> It doesn't and I'd like to know what I can do about it
>
> The difference between the two is simply that I've replaced 2 lines in
> class Panel
> with 2 lines in class MyFrame

Normal sizer automatic layout happens when the window owning the sizer
gets a size event. Since in your ON_THE_FLY case the window is already
at its final size there is not another size event after you add the 2nd
panel, so there is no automatic layout. That doesn't mean that you
can't do it yourself however, just add a call to self.panel.Layout.

BTW, when sending sample code with long lines please send it as an
attachment, not a copy and paste. I don't have time to undo word-wrap
just to run a sample.