Working with Widget after Instance loses the reference

Al in Dallas alfargnoli at yahoo.com
Tue Aug 1 11:04:02 EDT 2006


John McMonagle wrote:
> On Mon, 2006-07-31 at 11:15 -0700, Al in Dallas wrote:

[example of "losing" a widget]

> Consider the following code run in the python shell:
>
> >>> from Tkinter import *
> >>> r = Tk()
> >>> b1 = Button(r, text='test')
> >>> b1.pack()
> >>> b2 = Button(r, text='test2')
> >>> b2.pack()
> >>> r.children
> {'-1210160564': <Tkinter.Button instance at 0xb7de6a4c>, '-1210225748':
> <Tkinter.Button instance at 0xb7dd6bac>}
> >>> r.slaves()
> [<Tkinter.Button instance at 0xb7dd6bac>, <Tkinter.Button instance at
> 0xb7de6a4c>]
> >>> b1 = 'xxx'
> >>> b1.destroy()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: 'str' object has no attribute 'destroy'
> >>> b1 = r.slaves()[0]
> >>> b1.destroy()
> >>>
>
>
> So, as long as you know what your widget instance is in root.slaves() or
> root.children you can assign it to a new name.

Since I've been leaving my shell open, I jumped in and tried:

recoveredlabel = root.slaves()[6]

And after I verified I could manipulate the widget with that
name, I executed:

<original-widget-name> = recoveredlabel

Now the only difference between where I was (before I screwed up)
and where I am is that I've got this extra variable named
"recoveredlabel."

Thanks.

Now, do you have any advice on learning the syntax for dealing
with Tix megawidgets in Python? I guess my alternative is to
learn how to use Elmer or SWIG so I can hide all the Python
I've inherited "under the hood" and write my GUI in Tcl/Tk.




More information about the Python-list mailing list