# Ruby/Tk

**URL:** <https://www.ruby-forum.com/t/ruby-tk/168083>\
**Category:** Ruby\
**Created:** [May 15, 2009, 4:46pm UTC](https://www.ruby-forum.com/t/ruby-tk/168083 "2009-05-15T16:46:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![graycardinal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/54ee81/32.png) [@graycardinal](https://www.ruby-forum.com/u/graycardinal)\
**Post date:** [May 15, 2009, 4:46pm UTC](https://www.ruby-forum.com/t/ruby-tk/168083/1 "2009-05-15T16:46:02Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![graycardinal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/54ee81/32.png) [@graycardinal](https://www.ruby-forum.com/u/graycardinal)\
**Post date:** [May 16, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/ruby-tk/168083/2 "2009-05-16T14:25:43Z")

</div>

> How to get @edit value after “top” is closed ?

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

* * *

Diego

---

<div class="post-metadata">

**Author:** ![graycardinal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/54ee81/32.png) [@graycardinal](https://www.ruby-forum.com/u/graycardinal)\
**Post date:** [May 16, 2009, 8:28pm UTC](https://www.ruby-forum.com/t/ruby-tk/168083/3 "2009-05-16T20:28:36Z")

</div>

From: “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Ruby/Tk  
Date: Fri, 15 May 2009 23:45:07 +0900  
Message-ID:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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.

---

<div class="post-metadata">

**Author:** ![graycardinal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/54ee81/32.png) [@graycardinal](https://www.ruby-forum.com/u/graycardinal)\
**Post date:** [May 17, 2009, 11:55am UTC](https://www.ruby-forum.com/t/ruby-tk/168083/4 "2009-05-17T11:55:43Z")

</div>

I not found -textvariable options in TkText ☹

---

<div class="post-metadata">

**Author:** ![graycardinal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/54ee81/32.png) [@graycardinal](https://www.ruby-forum.com/u/graycardinal)\
**Post date:** [May 17, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/ruby-tk/168083/5 "2009-05-17T17:25:30Z")

</div>

From: Oleg I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Ruby/Tk  
Date: Sun, 17 May 2009 18:55:47 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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
