Sizers Parent Child ...

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 32
Reputation: vmars is an unknown quantity at this point 
Solved Threads: 0
vmars vmars is offline Offline
Light Poster

Sizers Parent Child ...

 
0
  #1
Oct 28th, 2008
Greetings:
I am having trouble with reading the code in thread "Tutorial: GUI programming with wxPython" (http://www.daniweb.com/forums/post623598-3.html)
Where tut shows code:
  1.  
  2. import wx
  3.  
  4. """Example with sizers for dynamic resizing."""
  5.  
  6. app = wx.App(redirect=False)
  7.  
  8. window = wx.Frame(None, title = 'Sample GUI App',
  9. pos = (100,100), size = (400,500))
  10. background = wx.Panel(window)
  11.  
  12. loadBtn = wx.Button(background, label = 'Load')
  13. transferBtn = wx.Button(background, label = 'Transfer')
  14. inputArea = wx.TextCtrl(background)
  15. transferArea = wx.TextCtrl(background, style = wx.TE_READONLY | wx.TE_MULTILINE)
  16.  
  17. horizontalBox = wx.BoxSizer()
  18. horizontalBox.Add(inputArea, proportion = 1, border = 0)
  19. horizontalBox.Add(transferBtn, proportion = 0, border = 0)
  20. horizontalBox.Add(loadBtn, proportion = 0, border = 0)
  21.  
  22. verticalBox = wx.BoxSizer(wx.VERTICAL)
  23. verticalBox.Add(horizontalBox, proportion = 0, flag = wx.EXPAND, border = 0)
  24. verticalBox.Add(transferArea, proportion = 1, flag = wx.EXPAND, border = 0)
  25.  
  26. background.SetSizer(verticalBox)
  27. window.Show()
  28. app.MainLoop()

Guess my dyslexia is acting up:
It doesn't look like horizontalBox is the child of verticalBox.
"It looks like 'background' is the child of 'window' .
'loadBtn = wx.Button(background' = 'loadBtn' is the child of 'background' .
'horizontalBox.Add(inputArea' = 'horizontalBox' is the child of 'inputArea'
'verticalBox.Add(horizontalBox' = and 'verticalBox' is the child of 'horizontalBox' .
And is 'background.SetSizer(verticalBox)' saying verticalBox is parent of background?
Can someone straighten me out.
How do I properly read these statements.
Maybe if I knew the syntax of each statement.
Thanks!
ô¿ô
V e r n

WinXp sp2 , Boa 0.6.1 , Delphi5
http://www.flickr.com/photos/vmars956/
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: tyincali is an unknown quantity at this point 
Solved Threads: 6
tyincali tyincali is offline Offline
Light Poster

Re: Sizers Parent Child ...

 
1
  #2
Oct 28th, 2008
The hierarchy ends up like this:

  1. window
  2. background
  3. verticalBox
  4. horizontalBox
  5. inputArea
  6. transferBtn
  7. loadBtn
  8. transferArea

the loadBtn, transferBtn, inputArea, and transferArea objects are defined with:

  1. loadBtn = wx.Button(background, label = 'Load')
  2. transferBtn = wx.Button(background, label = 'Transfer')
  3. inputArea = wx.TextCtrl(background)
  4. transferArea = wx.TextCtrl(background, style = wx.TE_READONLY | wx.TE_MULTILINE)

the code right after that defines horizontalBox and adds loadBtn, transferBtn, and inputArea as it's children via the Add() method

  1. horizontalBox = wx.BoxSizer()
  2. horizontalBox.Add(inputArea, proportion = 1, border = 0)
  3. horizontalBox.Add(transferBtn, proportion = 0, border = 0)
  4. horizontalBox.Add(loadBtn, proportion = 0, border = 0)

Finally, you define verticalBox and make horizontalBox and transferArea its children through the same Add() method.

  1. verticalBox = wx.BoxSizer(wx.VERTICAL)
  2. verticalBox.Add(horizontalBox, proportion = 0, flag = wx.EXPAND, border = 0)
  3. verticalBox.Add(transferArea, proportion = 1, flag = wx.EXPAND, border = 0)
Last edited by tyincali; Oct 28th, 2008 at 9:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,070
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Sizers Parent Child ...

 
0
  #3
Oct 29th, 2008
tyincali explained that very nicely.

And is 'background.SetSizer(verticalBox)' saying verticalBox is parent of background?
You are simply setting the main sizer, background is still the parent. Remember that the first object inside the () is not necessarily the parent.

In your case only the widgets have a parent argument:
wx.Frame(parent, id, title, pos, size, style)
wx.Panel(parent, id, pos, size, style)
wx.Button(parent, id, label, pos, size, style)
wx.TextCtrl(parent, id, value, pos, size, style)

Method sizer.Add() has the following arguments:
sizer.Add(widget, proportion, flag, border)

A lot of the pos, size, style, border, flag arguments have defaults. When you use sizers you don't have to supply the position argument in most cases. It's the role of the sizer to put the widget in the correct position.
Last edited by vegaseat; Oct 29th, 2008 at 10:59 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC