# Questions about Tk - Newbie

**URL:** <https://www.ruby-forum.com/t/questions-about-tk-newbie/56540>\
**Category:** Ruby\
**Created:** [March 31, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/questions-about-tk-newbie/56540 "2006-03-31T23:28:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [March 31, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/questions-about-tk-newbie/56540/1 "2006-03-31T23:28:48Z")

</div>

Hey I’ve just started to learn my first GUI toolkit today. Tk 🙂

Well, I surfed around for some tutorials for a while but I only found  
one  
that looked good.  
Could you link me some good tutorials? I escpecially need some that  
gives  
examples on how a tk and ruby work together.

My biggiest interest in tk right now, is how I store a string given by  
the  
user from an entry in a ruby variable. So if that is not in included in  
a  
tutorial, could someone please give me an example 🙂

Thanks in advande 🙂

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [April 1, 2006, 3:05am UTC](https://www.ruby-forum.com/t/questions-about-tk-newbie/56540/2 "2006-04-01T03:05:04Z")

</div>

Okay, thanks. I have played a little around with it now, and I can get a  
TkText widget to display a variable. Neither through text or insert. How  
do  
you then do it?

2006/4/1, Chris A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [April 1, 2006, 1:52am UTC](https://www.ruby-forum.com/t/questions-about-tk-newbie/56540/3 "2006-04-01T01:52:14Z")

</div>

# This can be shortened at the expense of clarity.

require ‘tk’

root = TkRoot.new  
v = TkVariable.new(‘Foo’)  
e = TkEntry.new(root,‘textvariable’=\>v)  
e.pack  
b = TkButton.new(root,‘text’=\>‘PrintFoo’,‘command’=\>proc {print v.value

- “\n”})  
b.pack

Tk.mainloop

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [April 1, 2006, 6:22am UTC](https://www.ruby-forum.com/t/questions-about-tk-newbie/56540/4 "2006-04-01T06:22:16Z")

</div>

So TkText’s don’t have textvariable’s. They sore their own text  
(actually more than just text as it can contain widgets, colors,  
different fonts, etc.). Think of TkText and TkCanvas as being  
similar. Wheras TkText and TkEntry are not.

require ‘tk’

root = TkRoot.new  
t = TkText.new(root)  
t.insert(‘end’,“Hi, here is some text!\n”)  
v = t.get(‘1.0’,‘end’)  
print v + “\n”

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [April 1, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/questions-about-tk-newbie/56540/5 "2006-04-01T16:00:26Z")

</div>

Ok, but what is then common to do when you have a variable pointing to a  
string with 1-10 lines of text and you want the string displayed in an  
editable field?

2006/4/1, Chris A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [April 1, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/questions-about-tk-newbie/56540/6 "2006-04-01T17:08:22Z")

</div>

From: “Jeppe J.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Questions about Tk - Newbie  
Date: Sat, 1 Apr 2006 22:59:01 +0900  
Message-ID:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Ok, but what is then common to do when you have a variable pointing to a  
> string with 1-10 lines of text and you want the string displayed in an  
> editable field?

## Hmm… I don’t know why you need such realtime reflection. I think you may have better solution for your problem. However, if you really need it, for example,

requrie ‘tk’

v = TkVariable.new

TkLabel.new(:textvariable=\>v, :relief=\>:ridge).pack  
TkButton.new(:text=\>‘add str’, :command=\>proc{v.value += ‘str’}).pack  
t = TkText.new.pack

v.trace(‘w’, proc{t.value = v.value})  
t.bind(‘KeyPress’, proc{v.value = t.value})

Tk.mainloop

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [April 1, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/questions-about-tk-newbie/56540/7 "2006-04-01T18:30:59Z")

</div>

It’s for a sort of calculater that I’m writing (It’s supposed to make my  
math and physics exams less painful ), so I need to be able to edit some  
output, shown in the TkText field, before I process the output further.

Thank very much you for the example, I think it was just what I needed  
to  
make it work 🙂

2006/4/1, Hidetoshi NAGAI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
