Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'configure': 0.05; 'root': 0.05; "'',": 0.07; 'args': 0.07; 'attribute': 0.07; 'method.': 0.07; 'nicely': 0.07; 'none,': 0.07; "subject:' ": 0.07; 'tkinter': 0.07; 'attributes': 0.09; 'created,': 0.09; 'cursor': 0.09; 'methods,': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'strings.': 0.09; 'width': 0.09; 'works.': 0.09; 'wrapper': 0.09; '{},': 0.09; 'python': 0.11; 'jan': 0.12; 'changes': 0.15; "'0',": 0.16; "'class':": 0.16; "'cursor':": 0.16; "'height':": 0.16; "'width':": 0.16; '(implemented': 0.16; '0),': 0.16; 'ast': 0.16; 'command:': 0.16; 'dict': 0.16; 'dictionary,': 0.16; 'dictionary.': 0.16; 'objects.': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'root:': 0.16; 'subject: \n ': 0.16; 'subject:object': 0.16; 'subject:where': 0.16; 'tcl': 0.16; 'wrote:': 0.18; 'widget': 0.19; 'later': 0.20; 'not,': 0.20; '>>>': 0.22; 'import': 0.22; 'separate': 0.22; 'header:User- Agent:1': 0.23; 'proxy': 0.24; 'source': 0.25; 'options': 0.25; 'shown': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'wondering': 0.29; 'am,': 0.29; 'skip:( 20': 0.30; 'getting': 0.31; "skip:' 10": 0.31; '>>>>': 0.31; 'container': 0.31; 'keys': 0.31; 'class': 0.32; 'option': 0.32; 'screen': 0.34; 'created': 0.35; 'but': 0.35; 'height': 0.36; 'raising': 0.36; 'method': 0.36; 'turn': 0.37; 'window': 0.38; 'to:addr:python-list': 0.38; 'itself': 0.39; 'visual': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'flat': 0.60; 'affect': 0.61; 'color': 0.61; 'no.': 0.61; 'entire': 0.61; 'such': 0.63; 'acts': 0.74; 'other.': 0.75; 'dict.': 0.84; 'otten': 0.84; 'partial': 0.84; 'received:fios.verizon.net': 0.84; 'snapshot': 0.84; 'relief': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: I dont understand root['bg'] = 'red' where root is a tkinter.Tk object Date: Mon, 17 Nov 2014 14:48:57 -0500 References: <5469f8d3$0$5132$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <5469f8d3$0$5132$426a74cc@news.free.fr> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 113 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416253764 news.xs4all.nl 2917 [2001:888:2000:d::a6]:33455 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80980 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': , '_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', , ), 'relief': ('relief', 'relief', 'Relief', , 'flat'), 'bg': ('bg', '-background'), 'class': ('class', 'class', 'Class', 'Toplevel', 'Tk'), 'visual': ('visual', 'visual', 'Visual', '', ''), 'highlightthickness': ('highlightthickness', 'highlightThickness', 'HighlightThickness', , 0), 'borderwidth': ('borderwidth', 'borderWidth', 'BorderWidth', , 0), 'highlightbackground': ('highlightbackground', 'highlightBackground', 'HighlightBackground', , 'SystemButtonFace'), 'width': ('width', 'width', 'Width', , 0), 'container': ('container', 'container', 'Container', 0, 0), 'bd': ('bd', '-borderwidth'), 'background': ('background', 'background', 'Background', , 'SystemButtonFace'), 'cursor': ('cursor', 'cursor', 'Cursor', '', ''), 'highlightcolor': ('highlightcolor', 'highlightColor', 'HighlightColor', , 'SystemWindowFrame'), 'use': ('use', 'use', 'Use', '', ''), 'colormap': ('colormap', 'colormap', 'Colormap', '', ''), 'height': ('height', 'height', 'Height', , 0), 'padx': ('padx', 'padX', 'Pad', , ), '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