Narasimha.K.R.L wrote:
Hi! all. This is Narasimha.
I am just learning wxPython from the net tutorial available at wxPython tutorial - Python GUI programming in wxPython
In the "communicate.py" script from http://www.zetcode.com/wxpython/firststeps , I am unable to understand the 12th line i.e
self.text = parent.GetParent().rightPanel.text
I am having trouble understanding the dot convention properly.
The author gave the below explanation, but I am not able to understand it properly.
"Each widget has a parent argument. In our example, parent is a panel on which we display both left and right panels. By calling parent.GetParent() we get reference to the frame widget. The frame widget has reference to the rightPanel. Finally, right panel has reference to the static text widget."
For further convenience I have attached the "communicate.py" script to this mail.
By the way this is my software installed:
O.S: Ubuntu Linux 8.10 (Intrepid Ibex)
Python 2.5.2
wxPython 2.8.9.1
Python IDE: Dr.Python
I request anyone to clearly explain me the concept.
Thanks in advance, Narasimha
Let's re-write the code to be a little more verbose!
<code>
# The LeftPanel's top parent is the frame in the Communicate class
# the following gets a handle on the frame object (AKA: an instance)
frame = parent.GetParent()
# Here we grab an instance of the rightPanel who's parent is
# also the Communicate frame
rightPanel = frame.rightPanel
# now we can get access to the rightPanel's attributes (AKA properties)
# which in this case is the text widget
self.text = rightPanel.text
</code>
In wxPython there are various hierarchies. Typically you have one of the following:
wx.App --> wx.Frame --> wx.Panel --> wx.Sizer --> widgets
wx.App --> wx.Frame --> wx.Sizer --> wx.Panel --> wx.Sizer --> widgets
wx.App --> wx.Frame --> wx.Sizer --> wx.Panel --> wx.Sizer --> wx.Sizer --> widgets
Normally the wx.Frame or wx.Window is the top-most widget. Then you have a wx.Panel on the frame which is there to provide cross-platform look-and-feel. If you don't have the wx.Panel, your application will look weird on Windows. It also enables tab navigation (I think). Then if you want ultimate flexibility, you use sizer to contain the widgets that you put on the panel. Some people put the panel itself in a sizer to help control the refreshing and layout calls better.
Try to think of it as a series of boxes or rectangles in each other. The Widget Inspection Tool can help you understand it better: http://wiki.wxpython.org/Widget%20Inspection%20Tool
HTH
···
-------------------
Mike Driscoll
Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org