Forum: Ruby Questions about Tk - Newbie

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.
E17632fab00b930ea3b30e1b98f39675?d=identicon&s=25 Jeppe Jakobsen (Guest)
on 2006-03-31 23:28
(Received via mailing list)
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 :)
2e34f386b20cb7c11c4204d699babd78?d=identicon&s=25 Chris Alfeld (Guest)
on 2006-04-01 01:52
(Received via mailing list)
# 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
E17632fab00b930ea3b30e1b98f39675?d=identicon&s=25 Jeppe Jakobsen (Guest)
on 2006-04-01 03:05
(Received via mailing list)
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 Alfeld <chris.alfeld@gmail.com>:
2e34f386b20cb7c11c4204d699babd78?d=identicon&s=25 Chris Alfeld (Guest)
on 2006-04-01 06:22
(Received via mailing list)
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"
E17632fab00b930ea3b30e1b98f39675?d=identicon&s=25 Jeppe Jakobsen (Guest)
on 2006-04-01 16:00
(Received via mailing list)
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 Alfeld <chris.alfeld@gmail.com>:
E4f967492dbd03c526cc9b397e68021d?d=identicon&s=25 Hidetoshi NAGAI (Guest)
on 2006-04-01 17:08
(Received via mailing list)
From: "Jeppe Jakobsen" <jeppe88@gmail.com>
Subject: Re: Questions about Tk - Newbie
Date: Sat, 1 Apr 2006 22:59:01 +0900
Message-ID:
<99b4ad3b0604010558n30ce10c2pd4ae3b5986cf0b6a@mail.gmail.com>
> 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
E17632fab00b930ea3b30e1b98f39675?d=identicon&s=25 Jeppe Jakobsen (Guest)
on 2006-04-01 18:30
(Received via mailing list)
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 <nagai@ai.kyutech.ac.jp>:
This topic is locked and can not be replied to.