Getting Tkinter Text contents before destruction

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

    #1

    Getting Tkinter Text contents before destruction

    Hi!

    I want to grab the contents of a Text widget when the frame it's on gets
    destroyed. I tried TextWidget.bind ("<Destroy>" ... , but the widget is gone
    before the call gets made, and I'd really hate to do something with the
    function that gets called with TextWidgetsFram e.bind("<Destro y>", ..., since
    that one function handles all of the frames in the program...or would that
    even work?

    How can I do this?

    Thanks!

    Bob


  • James Stroud

    #2
    Re: Getting Tkinter Text contents before destruction

    Bob Greschke wrote:[color=blue]
    > Hi!
    >
    > I want to grab the contents of a Text widget when the frame it's on gets
    > destroyed. I tried TextWidget.bind ("<Destroy>" ... , but the widget is gone
    > before the call gets made, and I'd really hate to do something with the
    > function that gets called with TextWidgetsFram e.bind("<Destro y>", ..., since
    > that one function handles all of the frames in the program...or would that
    > even work?
    >
    > How can I do this?
    >
    > Thanks!
    >
    > Bob
    >
    >[/color]

    If TextWidgetsFram e inherets from frame, you can override the destroy()
    method which gets called when the parent gets destroyed. Or
    alternatively, you can override the __del__ function, which gets called
    when the reference count goes to 0:


    py> from Tkinter import *
    py> tk = Tk()
    py> class F(Frame):
    .... def destroy(self):
    .... print 'bob'
    .... Frame.destroy(s elf)
    ....
    py> f = F(tk)
    py> f.pack()
    py> tk.destroy()
    bob

    James

    Comment

    • Eric Brunel

      #3
      Re: Getting Tkinter Text contents before destruction

      On Mon, 06 Feb 2006 19:32:52 -0800, James Stroud <jstroud@ucla.e du> wrote:[color=blue]
      > If TextWidgetsFram e inherets from frame, you can override the destroy()
      > method which gets called when the parent gets destroyed.[/color]

      Unfortunately, it doesn't get called. Everything actually happens at tk
      level, where the destroy *command* gets called, but doesn't inform the
      Python interface object. The destroy method is only a way to call tk's
      destroy command, but overloading it has an effect only when called from
      Python, not from the underlying tk layer.
      --
      python -c "print ''.join([chr(154 - ord(c)) for c in
      'U(17zX(%,5.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"

      Comment

      • Eric Brunel

        #4
        Re: Getting Tkinter Text contents before destruction

        On Mon, 6 Feb 2006 20:11:28 -0700, Bob Greschke <bob@greschke.c om> wrote:
        [color=blue]
        > Hi!
        >
        > I want to grab the contents of a Text widget when the frame it's on gets
        > destroyed. I tried TextWidget.bind ("<Destroy>" ... , but the widget is
        > gone
        > before the call gets made, and I'd really hate to do something with the
        > function that gets called with TextWidgetsFram e.bind("<Destro y>", ...,
        > since
        > that one function handles all of the frames in the program...or would
        > that
        > even work?
        >
        > How can I do this?[/color]

        One way is to define the deletion callback for the text's parent window to
        get the text before the widget gets deleted. To do that, you can use
        text.winfo_topl evel() to get the parent Toplevel for your text widget,
        then define the callback via wdw.protocol('W M_DELETE_WINDOW ', ...). Here
        is a detailed example:

        -----------------------------------------------------
        from Tkinter import *

        root = Tk()

        txt = None

        def openWdw():
        global txt
        wdw = Toplevel()
        frm = Frame(wdw)
        frm.pack(expand =1)
        txt = Text(frm)
        txt.pack()
        print txt.winfo_tople vel(), frm, root
        txt.winfo_tople vel().protocol( 'WM_DELETE_WIND OW', getText)

        def getText():
        print txt.get(1.0, END)
        txt.winfo_tople vel().destroy()

        Button(root, text='Go', command=openWdw ).pack()

        root.mainloop()
        -----------------------------------------------------

        This will of course only work if the only reason for which the text widget
        can be destroyed is if its parent window is closed.

        HTH
        --
        python -c "print ''.join([chr(154 - ord(c)) for c in
        'U(17zX(%,5.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"

        Comment

        • Fredrik Lundh

          #5
          Re: Getting Tkinter Text contents before destruction

          Bob Greschke wrote:
          [color=blue]
          > I want to grab the contents of a Text widget when the frame it's on gets destroyed. I tried
          > TextWidget.bind ("<Destroy>" ... , but the widget is gone before the call gets made, and I'd really
          > hate to do something with the function that gets called with TextWidgetsFram e.bind("<Destro y>",
          > ..., since that one function handles all of the frames in the program...or would that even work?
          >
          > How can I do this?[/color]

          in what ways can the frame be destroyed ?

          assuming that you're talking about user-initiated actions, the most reasonable way
          to do this is to implement a WM_DELETE_WINDO W handler on the toplevel
          window that the frame is located in, and deal with the text widget in there. see:



          </F>



          Comment

          Working...