473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

access properties of parent widget in Tkinter

I am trying to get & set the properties of a widget's parent widget.
What I have works, but seems like a long way around the block. First I
get the widget name using w.winfo_parent(), then i convert the name to a
reference using nametowidget().

self.nametowidget(event.widget.winfo_parent()).has Changed= True

It works, but I can't help but think I'm missing a more direct route. Am I?

Bill
Jul 19 '05 #1
6 10126
William Gill wrote:
I am trying to get & set the properties of a widget's parent widget.
What I have works, but seems like a long way around the block. First I
get the widget name using w.winfo_parent(), then i convert the name to a
reference using nametowidget().

self.nametowidget(event.widget.winfo_parent()).has Changed= True


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?

Kent
Jul 19 '05 #2
Kent Johnson wrote:
William Gill wrote:
I am trying to get & set the properties of a widget's parent widget.
What I have works, but seems like a long way around the block. First
I get the widget name using w.winfo_parent(), then i convert the name
to a reference using nametowidget().

self.nametowidget(event.widget.winfo_parent()).has Changed= True

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?

Kent


Maybe I should clarify what I am doing and why, so that I can get a
better understanding of your replies.
The overall app is a gui editor for a database made up of several
related tables; one for basic info, one for their various phone numbers
(publish, contact, fax,cell, etc), one for various addresses (publish
,ship to, mail to, etc.), one for the services they subscribe to, etc.
The editor brings records from each of the various tables together in
one “record” for editing. Each table has an associated python/tkinter
class (widget) that displays the fields in a parent widget, and each
field is a child of the table widget. So “city” is a child of address,
is a child of client. If I change or add a phone number that db table
will need updating when I’m done, but the address table may not. I have
designed each table class with a hasChanged attribute, initialized to
False, if a child Entry widget changes I set has changed to True. When
I’m done the exit routine runs each table objects save routine, which
checks hasChanged to see if an update query needs to be run on that table.

Client
- basicData object (data from client table)
- company
- contact person
- notes
- phones object (data from phonenumber table)
- type (cell, fax, publish, etc)
- areaCode
- exchange
- base

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.
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? Besides, I need to
pass the state of areaCode (has it changed or not), not its value?
Bill

Jul 19 '05 #3
Kent Johnson wrote:
William Gill wrote:
I am trying to get & set the properties of a widget's parent widget.
What I have works, but seems like a long way around the block. First
I get the widget name using w.winfo_parent(), then i convert the name
to a reference using nametowidget().

self.nametowidget(event.widget.winfo_parent()).has Changed= True

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?

Kent


A little more research into stringVar, and the stringVar.trace() method
may be one way to go. It may be simpler, or more convoluted having so
many stringVar objects. I'll do some experimenting, but I'm still open
to other ideas.

Bill
Jul 19 '05 #4
Kent Johnson wrote:
William Gill wrote:
I am trying to get & set the properties of a widget's parent widget.
What I have works, but seems like a long way around the block. First
I get the widget name using w.winfo_parent(), then i convert the name
to a reference using nametowidget().

self.nametowidget(event.widget.winfo_parent()).has Changed= True

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?

Kent


I stumbled across the widget.master attribute. so

self.nametowidget(event.widget.winfo_parent()).has Changed = True

can be re-written as

self.master.hasChanged = True

I thought I was doing things the indirect way.

Thanks everyone for your help.

I'm still interested in if anyone thinks this is "bad practice", and why.

Bill
Jul 19 '05 #5
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

Jul 19 '05 #6
Kent Johnson wrote:
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

I will digest this some more, and see if the polling of child attributes
gets harder to follow than setting parent attributes.

In theory it makes sense to have the independence you discuss, in this
case it's hard to imaging needing the areaCode widget without the
(parent) phonenumber widget, but this is the exception. So your
suggestion makes more general sense.

By the way, my suspicion was right, self.master.hasChanged = True ,
provides direct access, but now I'm not sure that's the way to go.

Thanks,

Bill
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Harlin Seritt | last post by:
I am working on a Find Text dialog box. Once you find a string in a Text widget, how do you at least move the cursor to that index (position)? Even better how can one 'select' the string one finds?...
0
by: William Gill | last post by:
I have a Tkinter (frame) widget that contains several other frame widgets, each containing entry widgets. In the parent frame I have a 'save' button that is initially disabled. As it is now, each...
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
2
by: Jon Hyland | last post by:
This might be a dumb question, but what is the best way for one instance of a user control to access properties of an instance of another user control? For example, let's say I have an instance...
0
by: Tony | last post by:
Hi, I'm trying to access properties from a masterpage. With no success. Can somebody help me please? I used the example at:...
3
by: Antuane | last post by:
hi guys, its me again, with a few simple (probably silly) quesetion... hi guys, i've managed to create a custom textbox class. & i'd like to access the parent control (in this case this would be...
1
by: CT | last post by:
Hi, Looking for an API that provides directory + file access properties. There is a lot of ansii functions that returns only the properties of a file/directory, i'm more interested in security,...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I'm sorry for such a basic question, but here it is: I'm creating a VS2005 C# forms application. In the parent form I have a button that opens a child form. In the code for the parent...
22
by: waltapp | last post by:
Hello, I am working in javascript and I need to access the parent element. I have 4 different html documents in the same folder. The names are title, code, golf,tree. I am currently in the title...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.