Working with Widget after Instance loses the reference

Discussion in 'Python' started by Al in Dallas, Jul 31, 2006.

  1. Al in Dallas

    Al in Dallas Guest

    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.
     
    Al in Dallas, Jul 31, 2006
    #1
    1. Advertising

  2. 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.
     
    John McMonagle, Aug 1, 2006
    #2
    1. Advertising

  3. Al in Dallas

    Al in Dallas Guest

    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.
     
    Al in Dallas, Aug 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    343
  2. Frederic Rentsch
    Replies:
    0
    Views:
    258
    Frederic Rentsch
    Jul 8, 2012
  3. Terry Reedy
    Replies:
    15
    Views:
    647
    Frederic Rentsch
    Jul 16, 2012
  4. Replies:
    8
    Views:
    294
    Ian Kelly
    Jul 24, 2012
  5. Replies:
    3
    Views:
    250
Loading...

Share This Page