I dont understand root['bg'] = 'red' where root is a tkinter.Tk object

Terry Reedy tjreedy at udel.edu
Mon Nov 17 20:48:57 CET 2014


On 11/17/2014 8:32 AM, ast wrote:
> Hello,
>
> import tkinter
> root = tkinter.Tk()
>
> Let's see all attributes of root:
>
>>>> root.__dict__
> {'master': None, 'children': {}, '_tclCommands': ['tkerror', 'exit',
> '13825848destroy'], 'tk': <tkapp object at 0x02949C28>, '_tkloaded': 1}
>
> Now we change the background color using following command:

As Peter Otten nicely explained, tkinter widgets are proxies for tk 
widgets, with a few extra attributes added (as shown above).  Tk widget 
methods are proxied as tkinter widget methods, with Python syntax and 
objects.

Tk widgets each *have* an internal options dictionary, created with the 
widget and later accessed trough the .configure method.  As tcl 
dictionaries, keys and values are strings. The configure method with no 
args returns the entire options dictionary and the Python wrapper return 
it as a Python dict.  Each value is a 2- or 5-tuple.

 >>> root.configure()
{'takefocus': ('takefocus', 'takeFocus', 'TakeFocus', '0', '0'), 'pady': 
('pady', 'padY', 'Pad', <pixel object: '0'>, <pixel object: '0'>), 
'relief': ('relief', 'relief', 'Relief', <index object: 'flat'>, 
'flat'), 'bg': ('bg', '-background'), 'class': ('class', 'class', 
'Class', 'Toplevel', 'Tk'), 'visual': ('visual', 'visual', 'Visual', '', 
''), 'highlightthickness': ('highlightthickness', 'highlightThickness', 
'HighlightThickness', <pixel object: '0'>, 0), 'borderwidth': 
('borderwidth', 'borderWidth', 'BorderWidth', <pixel object: '0'>, 0), 
'highlightbackground': ('highlightbackground', 'highlightBackground', 
'HighlightBackground', <color object: 'SystemButtonFace'>, 
'SystemButtonFace'), 'width': ('width', 'width', 'Width', <pixel object: 
'0'>, 0), 'container': ('container', 'container', 'Container', 0, 0), 
'bd': ('bd', '-borderwidth'), 'background': ('background', 'background', 
'Background', <border object: 'SystemButtonFace'>, 'SystemButtonFace'), 
'cursor': ('cursor', 'cursor', 'Cursor', '', ''), 'highlightcolor': 
('highlightcolor', 'highlightColor', 'HighlightColor', <color object: 
'SystemWindowFrame'>, 'SystemWindowFrame'), 'use': ('use', 'use', 'Use', 
'', ''), 'colormap': ('colormap', 'colormap', 'Colormap', '', ''), 
'height': ('height', 'height', 'Height', <pixel object: '0'>, 0), 
'padx': ('padx', 'padX', 'Pad', <pixel object: '0'>, <pixel object: 
'0'>), 'menu': ('menu', 'menu', 'Menu', '', ''), 'screen': ('screen', 
'screen', 'Screen', '', '')}

Once created, this Python snapshot is independent of the tk options 
dictionary.  Changes in either do not affect the other.

Since sometime in 2.x, each tkinter widget *also* acts as a partial 
proxy for the tk widget's option dict.  Getting and setting a value by 
key works.

 >>> root['bg']
'SystemButtonFace'

>>>> root['bg'] = 'red'

(Interior of root window turn bright red.)
 >>> root['bg']
'red'

The keys method works. (I did not notice until reading the source in 
Peter's reply.)

 >>> root.keys()
['bd', 'borderwidth', 'class', 'menu', 'relief', 'screen', 'use', 
'background', 'bg', 'colormap', 'container', 'cursor', 'height', 
'highlightbackground', 'highlightcolor', 'highlightthickness', 'padx', 
'pady', 'takefocus', 'visual', 'width']

Other dict methods, such as .items(), do not, instead raising 
AttributeError.  But items can be simulated.

 >>> for k in sorted(root.keys()):
	print(k, root[k])
	
background red
bd 0
bg red
borderwidth 0
class Tk
colormap
container 0
cursor
height 0
highlightbackground SystemButtonFace
highlightcolor SystemWindowFrame
highlightthickness 0
menu
padx 0
pady 0
relief flat
screen
takefocus 0
use
visual
width 0

> I am wondering what 'bg' is for object root. Is it an attribute ?

No.  It is a key of one of the key,value pairs in the dict that root 
acts like.  This is the same as for any dict.  The attribute namespace 
of the dict (implemented by .__dict__) is separate from the namespace 
that the dict itself implements.  Given d = {1:'one'}, 1 is not an 
attribute of d.

-- 
Terry Jan Reedy




More information about the Python-list mailing list