How to find an object existing?

Terry Reedy tjreedy at udel.edu
Thu Jun 14 02:42:28 EDT 2018


On 6/14/2018 1:20 AM, Christian Gollwitzer wrote:
> Am 14.06.18 um 05:14 schrieb huey.y.jiang at gmail.com:
>> root = Tkinter.Tk()
>> button = Tkinter.Button(root, text="Find me")
>> button.pack()
>>
>> I created a button, Python object. I recall I can check this object 
>> existing by using   winfo, such as  winfo.exists(button). However, I 
>> forgot which super class contains this   winfo   method.  I printed 
>> out dir(os), dir(sys), dir(Tkinter), but did not find this winfo 
>> method. I wonder who will be kindly drop down a few lines? 
> This is due to a bad wrapping of the original Tcl interface of Tk. In 
> the original Tcl, "winfo" is not a method of anything, it is a free 
> function. In TkInter, all free functions that accept a widget as the 
> first parameter were made methods of that widget.

They were made methods of all widgets.  Thanks for pointing this out, as 
it same some anomalies more understandable.

Someone was overcome with Java-esque OO-itis, the notion that all 
function have to be methods.
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html
calls them universal methods.  The result is that help(Text), for 
instance, returns over 1000 lines because it includes all the universal 
methods in addition to the real Text methods. dir(Text(root)) is also 
overburdened with non-Text information.

> Therefore, 
> "winfo_exists" is a method of "button", which is logically contorted: 
> you are asking an object "do you exist?".

You are actually asking the python instance whether the tk window it 
initially represented still exists.  In tk, the argument is a window 
name that represents a window structure.  Does the structure initially 
represented by a name still exist?

This is similar to a python file instance myfile existing after the 
corresponding OS file has been closed (destroyed) with myfile.close. 
This sets the attribute myfile.closed to True.  Calling other file 
methods fails because there is no OS structure to relay it to.

> However, since it can be explicitly destroyed, this can still be useful:
> 
>  >>> import Tkinter
>  >>> root = Tkinter.Tk()
>  >>> button = Tkinter.Button(root, text="Find me")
>  >>> button.pack()
>  >>> button.winfo_exists()
> 1
>  >>> button.destroy()
>  >>> button.winfo_exists()
> 0
> 
> I am not sure that this is very helpful, though. Usually you don't 
> destroy a button yourself, it is done by the destructor when the button 
> is deleted, and therefore, winfo_exists() under normal circumstances 
> returns true.

The reference above omits winfo-exists, perhaps because it is so useless.

-- 
Terry Jan Reedy





More information about the Python-list mailing list