943,575 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 748
  • Python RSS
Oct 28th, 2008
0

Sizers Parent Child ...

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Light Poster
vmars is offline Offline
38 posts
since Oct 2008
Oct 28th, 2008
1

Re: Sizers Parent Child ...

The hierarchy ends up like this:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  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.

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 29th, 2008
0

Re: Sizers Parent Child ...

tyincali explained that very nicely.

Quote ...
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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Simple problem... please help!
Next Thread in Python Forum Timeline: wxpython combining images with other things





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC