Forum: Ruby Ruby/Tk

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9c2c4266dd6696a11323df12e28ff57?d=identicon&s=25 Oleg Igor (graycardinal)
on 2009-05-15 16:46
(Received via mailing list)
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 ?
Aae5c9f81fe990a48daf2bb91ac82759?d=identicon&s=25 Diego Virasoro (Guest)
on 2009-05-16 14:25
(Received via mailing list)
> How to get @edit value after "top" is closed ?

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

---
Diego
E4f967492dbd03c526cc9b397e68021d?d=identicon&s=25 Hidetoshi NAGAI (Guest)
on 2009-05-16 20:28
(Received via mailing list)
From: "graycardinalster@gmail.com" <graycardinalster@gmail.com>
Subject: Ruby/Tk
Date: Fri, 15 May 2009 23:45:07 +0900
Message-ID:
<00f13bf8-211a-4a9c-8355-b6f30a38856a@z19g2000vbz.googlegroups.com>
> 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.
B9c2c4266dd6696a11323df12e28ff57?d=identicon&s=25 Oleg Igor (graycardinal)
on 2009-05-17 11:55
I not found -textvariable options in TkText :(
E4f967492dbd03c526cc9b397e68021d?d=identicon&s=25 Hidetoshi NAGAI (Guest)
on 2009-05-17 17:25
(Received via mailing list)
From: Oleg Igor <graycardinalster@gmail.com>
Subject: Re: Ruby/Tk
Date: Sun, 17 May 2009 18:55:47 +0900
Message-ID: <9a4197f4c584ffc1c50449c46246739d@ruby-forum.com>
> 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
This topic is locked and can not be replied to.