Problem understanding the script

Hi! all. This is Narasimha.
I am just learning wxPython from the net tutorial available at http://www.zetcode.com/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

communicate.py (1.47 KB)

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

Hello Narasimha,

I think you come across the same difficulty I had myself a while ago
and which was clearly answered by Cody, Mike, and others.

You may spend 5 minutes reading this post:

Hope this helps.
Rene

···

On Wed, Jan 14, 2009 at 3:57 PM, Narasimha.K.R.L <krsimha@gmail.com> 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

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

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 think this is an excellent example of what *not* to do and why reducing the coupling between components in an application is so important. The left panel should not need to know that the parent has something named rightPanel nor that it has an attribute named text. If there is ever any updates to the implementation of RightPanel then it is required to update (and fully retest) LeftPanel too.

···

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

Robin Dunn wrote:

self.text = parent.GetParent().rightPanel.text

I think this is an excellent example of what *not* to do

exactly:

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Robin Dunn wrote:

self.text = parent.GetParent().rightPanel.text

I think this is an excellent example of what *not* to do

exactly:

Law of Demeter - Wikipedia

-Chris

Hi Chris. You've referenced that Wikipedia page before and I have glanced
at it but for whatever reason just now I read it more and grok it better. It
has a number of nice ways to explain the concept, including the rephrasing
of it as "use only one dot" (in dot notation). Thanks, again. (I have been
trying to refactor code lately, and really want to keep this in mind).

Che

···

On Wed, Jan 14, 2009 at 5:15 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

I thank all the 'wxpython-users' members for helping me understand the
script "communicate.py". It is some what easy than I thought otherwise.
Especially I thank "Mike Driscoll" and "Rene Heymans" for their 'to the
point' answers.

Thanks Once again,
Narasimha

Thank you Narasimha for your kind note.
This is the beauty of people sharing their knowledge.
We are all indebted to others, like others will be to us.
So it goes from one generation to the next.
Kind regards,
Rene

···

On Thu, Jan 15, 2009 at 1:13 PM, Narasimha.K.R.L <krsimha@gmail.com> wrote:

I thank all the 'wxpython-users' members for helping me understand the
script "communicate.py". It is some what easy than I thought otherwise.
Especially I thank "Mike Driscoll" and "Rene Heymans" for their 'to the
point' answers.

Thanks Once again,
Narasimha

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users