access properties of parent widget in Tkinter

Kent Johnson kent37 at tds.net
Thu Jun 16 13:44:47 EDT 2005


William Gill wrote:
> Kent Johnson wrote:
>
> If I change the area code in one record only the phonenumber table needs 
> to be updated, but since areaCode is a child of phones, 
> phones.hasChanged needs to be set to True by the areaCode entry widget.

One possibility is for the phones.hasChanged to poll the hasChanged attributes of its children. In other words, instead of propagating hasChanged up when a change occurs, poll the dependent widgets when you need to know.

Another way would be to pass a calllback to the child widget that it calls when it is changed, or to set up some kind of event mechanism. In Java I often set the parent to monitor property change events of the child.

> 
>  > Personally I think it is bad design for a widget to assume anything
>  > about its enclosing environment. What about passing a StringVar or
>  > IntVar to the child widget and letting it work with that?
> 
> This is not a criticism, but how is passing a known intVar or stringVar 
> any different than designing with the knowledge that an areaCode widget 
> can not exist except as a child of phonenumber?   

When you pass a stringVar to the child, it knows only what it is told, it is not assuming that some incantation on its parent will do the right thing. This has a direct effect on reusability of the child in other contexts. You may think you don't want to reuse the child, but it is very handy to be able to make a small test harness for a widget rather than firing up the whole application, navigating to the correct screen, etc. It is much harder to do this if the widget depends on its enclosing environment. And I find that if I design widgets for reuse, other uses tend to come up often enough to make it worth-while.

It's not that different from relying on any other global state. It makes code harder to understand, harder to test and harder to reuse.

More generally, IMO it is good practice to try to make acyclic dependency graphs between classes and modules, so if the parent depends on the child, the child shouldn't depend on the parent.

> Besides, I need to 
> pass the state of areaCode (has it changed or not), not its value?

OK, I misunderstood your original post on that point.

Kent
> 



More information about the Python-list mailing list