Ruby/Tk

Discussion in 'Ruby' started by graycardinalster@gmail.com, May 15, 2009.

  1. Guest

    Hi !

    I have a trouble.
    ###
    require "tk"

    class Main
    def initialize
    @root = TkRoot.new { title "prog" }

    top = TkToplevel.new
    @edit = TkEntry.new(top) {
    pack
    }

    cmd = proc { puts @edit.get }

    button = TkButton.new(@root) {
    pack
    text ""
    command cmd
    }

    top.grab
    Tk.mainloop
    end
    end

    m = Main.new
    ###
    How to get @edit value after "top" is closed ?
     
    , May 15, 2009
    #1
    1. Advertising

  2. > How to get @edit value after "top" is closed ?

    If I remember correctly you need to use @edit.value

    ---
    Diego
     
    Diego Virasoro, May 16, 2009
    #2
    1. Advertising

  3. From: "" <>
    Subject: Ruby/Tk
    Date: Fri, 15 May 2009 23:45:07 +0900
    Message-ID: <>
    > How to get @edit value after "top" is closed ?


    One of the simple solution is to use a TkVariable and
    'textvariable' option of an entry widget.
    ------------------------------------------
    @edit_var = TkVariable.new
    @edit = TkEntry.new(top, :textvariable=>@edit_var).pack
    :
    cmd = proc{ puts @edit_var.value }
    ------------------------------------------
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, May 16, 2009
    #3
  4. Oleg Igor Guest

    Oleg Igor, May 17, 2009
    #4
  5. From: Oleg Igor <>
    Subject: Re: Ruby/Tk
    Date: Sun, 17 May 2009 18:55:47 +0900
    Message-ID: <>
    > I not found -textvariable options in TkText :(


    If you use Tcl/Tk8.5, Tk::Text::peer widget is useful.
    ----------------------------------------------------------------------
    txt = Tk::Text.new # A base text widget.
    # In this case, it isn't shown on the GUI.

    top = Tk::Toplevel.new
    peer = Tk::Text::peer.new(txt, top).pack
    # A peer widget of 'txt' widget. Its parent is 'top' widget.

    # ... destroy 'top' widget ...

    p txt.value # This is the same content with the destroyed peer.
    ----------------------------------------------------------------------

    If you use Tcl/Tk8.4 or the former version,
    you can "withdraw" the dialog instead of "destroy".
    ----------------------------------------------------------------------
    top = Tk::Toplevel.new
    txt = Tk::Text.new(top).pack
    @value = ""
    top.protocol('WM_DELETE_WINDOW'){ top.withdraw; @value = txt.value }
    # Ignore 'WM_DELETE_WINDOW' protocol (e.g. when click the 'delete'
    # button on the window frame) from a window manager.
    # In this case, withdraw the toplevel widget instead of destroy it.

    # ... withdraw the toplevel ...

    p @value

    top.deiconify # Display the toplevel widget in normal form.
    ----------------------------------------------------------------------

    Or, get value before destroying the toplevel.
    ----------------------------------------------------------------------
    top = Tk::Toplevel.new
    txt = Tk::Text.new(top).pack
    @value = ""
    top.protocol('WM_DELETE_WINDOW'){ @value = txt.value; top.destroy }

    # ... destroy the toplevel ...

    p @value
    ----------------------------------------------------------------------
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, May 17, 2009
    #5
    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:
    222
  2. anne001
    Replies:
    1
    Views:
    525
  3. Phrogz
    Replies:
    4
    Views:
    244
    Austin Ziegler
    Sep 6, 2006
  4. roschler
    Replies:
    0
    Views:
    188
    roschler
    Oct 16, 2006
  5. Nicholas
    Replies:
    3
    Views:
    397
    Ryan Davis
    Jan 28, 2007
Loading...

Share This Page