Working with Widget after Instance loses the reference
John McMonagle
jmcmonagle at velseis.com.au
Mon Jul 31 19:47:54 EDT 2006
On Mon, 2006-07-31 at 11:15 -0700, Al in Dallas wrote:
> I made the mistake of creating an instance of a widget and assigning it
> to a name I'd already used. Now, if I use root.children or
> root.slaves(), I can see the "lost" widget, but can I do anything else
> with the string of numbers that shows up when I use root.children? I'd
> like to destory the widget, for example. it would be even better if I
> could create a new name and have it reference the "lost" widget.
>
> Of course, I can just kill my toplevel and start over.
>
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.
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
More information about the Python-list
mailing list