473,789 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.nametowidg et(event.widget .winfo_parent() ).hasChanged= 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 10310
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.nametowidg et(event.widget .winfo_parent() ).hasChanged= 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.nametowidg et(event.widget .winfo_parent() ).hasChanged= 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.hasChang ed 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.nametowidg et(event.widget .winfo_parent() ).hasChanged= 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.nametowidg et(event.widget .winfo_parent() ).hasChanged= 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.nametowidg et(event.widget .winfo_parent() ).hasChanged = True

can be re-written as

self.master.has Changed = 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.hasChang ed needs to be set to True by the areaCode entry widget.
One possibility is for the phones.hasChang ed 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.hasChang ed needs to be set to True by the areaCode entry widget.

One possibility is for the phones.hasChang ed 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.has Changed = 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
2821
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? ---code--- def searchText(): while 1: pos = self.mainEdit.search(findString.get(), 1.0, stopindex=END) if not pos:
0
1154
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 widget has a hasChanged property that I can poll to see if updates to the source data need to be made. hasChanged is set to True by an event routine in each frame widget, and this works fine for my exit routine which knows to poll each widget...
2
11258
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
2358
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 of UserControlA (ucA1) and an instance of UserControlB (ucB1) on a particular ASP.NET page. UserControlA has a string property called StringA that gets generated from a DB call in it's Page_Load event. I want UserControlB to be able to access...
0
1106
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: http://msdn.microsoft.com/library/en-us/dnvs05/html/masterpages.asp?frame=false#masterpages_topic11 (Exposing Properties and Methods from a Master Page) here my code:
3
1847
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 the form, cuz i drop the textbox onto the form), in the constructor of the custom textbox class. i'd like to access the parent control in the constructur of the textbox. But i can't do this, cuz i get an error, saying that the parent property...
1
1468
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, network sharing access. thanks,
1
2254
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 form I have some public objects and some public methods. How can the child code access those objects and methods? I know that the child has access to the parent properties by using this.Parent, but the only members I see in the parent are the...
22
5299
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 document and I want to access a frame in the code document. I am new to programming in javascript and I know this is simple. Can someone please point me in the right direction.
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.