Clearing the Tkinter Window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dustan

    #1

    Clearing the Tkinter Window

    I'm a newbie here, especially with Tkinter. I'm writing a program that
    has 3 phases, if you will, in which I would have to clear the window
    and insert new widgets. Is this possible, and if so, how? I'm writing
    my application class based on Frame, if that helps at all.

  • James Stroud

    #2
    Re: Clearing the Tkinter Window

    Dustan wrote:[color=blue]
    > I'm a newbie here, especially with Tkinter. I'm writing a program that
    > has 3 phases, if you will, in which I would have to clear the window
    > and insert new widgets. Is this possible, and if so, how? I'm writing
    > my application class based on Frame, if that helps at all.
    >[/color]

    Its not obvious what you are asking.

    But, if you populate a Frame with widgets, then you can destroy those
    widgets if you destroy the Frame:

    tk = Tk()
    f = Frame(tk)

    # fill frame with 10 buttons
    for i in xrange(10):
    Button(f, text=str(i)).pa ck()

    # kill the frame and buttons
    f.destroy() # references to buttons gone too, so they are GC'd

    # make a new frame and put it in tk
    f = Frame(tk)

    # etc.

    You will want to make sure any name assignments are re-assigned, del'd,
    or go out of scope after you destroy frame, or the button objects won't
    be garbage collected, even though they are destroyed when the frame is
    destroyed.

    James

    Comment

    • Martin Franklin

      #3
      Re: Clearing the Tkinter Window

      James Stroud wrote:[color=blue]
      > Dustan wrote:
      >[color=green]
      >>I'm a newbie here, especially with Tkinter. I'm writing a program that
      >>has 3 phases, if you will, in which I would have to clear the window
      >>and insert new widgets. Is this possible, and if so, how? I'm writing
      >>my application class based on Frame, if that helps at all.
      >>[/color]
      >
      >
      > Its not obvious what you are asking.
      >
      > But, if you populate a Frame with widgets, then you can destroy those
      > widgets if you destroy the Frame:
      >
      > tk = Tk()
      > f = Frame(tk)
      >
      > # fill frame with 10 buttons
      > for i in xrange(10):
      > Button(f, text=str(i)).pa ck()
      >
      > # kill the frame and buttons
      > f.destroy() # references to buttons gone too, so they are GC'd
      >
      > # make a new frame and put it in tk
      > f = Frame(tk)
      >
      > # etc.
      >
      > You will want to make sure any name assignments are re-assigned, del'd,
      > or go out of scope after you destroy frame, or the button objects won't
      > be garbage collected, even though they are destroyed when the frame is
      > destroyed.
      >
      > James[/color]


      destroy is one way to do it... but if you need to access the destroyed
      widgets information after they have gone it may cause problems.. another
      method to look at is (are) the *_forget methods (pack_forget /
      grid_forget) these do not destroy the widgets but rather remove them
      from view. If for example you are writing a 'wizard' type GUI and want
      to show the user the 'Next' page simply forget the current page and pack
      the next one in it's place.

      Another way is to grid each 'page' over the top of each other and simply
      change the 'raise' order (with tkraise method) not 100% sure if this
      works though, i've not used it directly myself...


      Cheers
      Martin

      Comment

      • Dustan

        #4
        Re: Clearing the Tkinter Window

        I don't want to destroy the root, I just want to remove the widgets
        (the exact opposite of what Martin was saying). I started working on
        James' idea, but it'll be a while before I have it fully implemented to
        test.

        Comment

        • Fredrik Lundh

          #5
          Re: Clearing the Tkinter Window

          "Dustan" <DustanGroups@g mail.com> wrote:
          [color=blue]
          >I don't want to destroy the root, I just want to remove the widgets
          > (the exact opposite of what Martin was saying). I started working on
          > James' idea, but it'll be a while before I have it fully implemented to
          > test.[/color]

          each widget has a "children" attribute, which contains a dictionary with Tk widget
          names as keys, and Python widget objects as values. to destroy all child widgets,
          you can simply do:

          for w in widget.children .values():
          w.destroy()

          where "widget" is the widget you want to reset.

          </F>



          Comment

          Working...