TkText no -textvariable workaround?

well, i’ve been busy adding tk support to my gui template tool (Miter),
but i’ve sadly come across a limitation with the TkText widget. it
dosen’t support -textvariable, hence there’s no way to bind it to a ruby
instance variable, as i can with all the other widgets. i did figure out
a way of automatically set the instance varaible when the TkText value
changes using bind. but i can not find a way to do the reverse. how do i
get the TkText’s value to automatically change when the instance
variable does? i looked to see if there’s was a ruby callback method for
such, but i didn’t find one. anyone?

···


~transami

(") dobee dobee do…
\v/
^ ^

Tom Sawyer wrote:

well, i’ve been busy adding tk support to my gui template tool (Miter),
but i’ve sadly come across a limitation with the TkText widget. it
dosen’t support -textvariable, hence there’s no way to bind it to a ruby
instance variable, as i can with all the other widgets. i did figure out
a way of automatically set the instance varaible when the TkText value
changes using bind. but i can not find a way to do the reverse. how do i
get the TkText’s value to automatically change when the instance
variable does? i looked to see if there’s was a ruby callback method for
such, but i didn’t find one. anyone?

With TkText you go…

@text = TkText.new(… blah blah blah …)

and then later on you can go

@text.value
@text.delete(1.0,‘end’)

and all that jazz.

is that what you wanted?

more along these lines:

text = TkVariable.new
widget = TkEntry.new(parent) { textvariable text1 }

then later on i can just say:

text = ‘whatever’

and the widget will automatically be set to ‘whatever’. but TkText
doesn’t support textvariable. so how do i do the same thing with it?

···

On Tue, 2002-07-16 at 08:29, Peter Hickman wrote:

With TkText you go…

@text = TkText.new(… blah blah blah …)

and then later on you can go

@text.value
@text.delete(1.0,‘end’)

and all that jazz.

is that what you wanted?


~transami

(") dobee dobee do…
\v/
^ ^

Tom Sawyer wrote:

With TkText you go…

@text = TkText.new(… blah blah blah …)

and then later on you can go

@text.value
@text.delete(1.0,‘end’)

and all that jazz.

is that what you wanted?

more along these lines:

text = TkVariable.new
widget = TkEntry.new(parent) { textvariable text1 }

then later on i can just say:

text = ‘whatever’

and the widget will automatically be set to ‘whatever’. but TkText
doesn’t support textvariable. so how do i do the same thing with it?

The widgets that use textvariable have it as an option, a TkLabel widget
can use textvariable if it likes, or it could use text.

The TkText widget however does not have this option, it makes little
sense to have text ‘This is the text’ so what you have is…

@text = TkText.new(…)

print @text.value # returns the contents of the variable

@text.insert(‘end’, line) # where line is a text string appends line to
the end of text

@text.insert(2.3, line) # inserts line at the third character of the
second line

@text.delete(1.0, ‘end’) # deletes all text from line one (the start of
the text), character 0 to the end of the document

@text.see(1.0) # moves the focus of the text widget to the first line of
text

So for what you want…

@text.delete(1.0, ‘end’) # to get rid of the existing data and
@text.insert(‘end’, line) # to insert the new text

The Perl/Tk pocket reference from O’Reilly is a usefull reference, if
somewhat terse. What you are seeing is more an artefact of Tk than Ruby.

···

On Tue, 2002-07-16 at 08:29, Peter Hickman wrote:

Hi,

···

From: Tom Sawyer transami@transami.net
Subject: Re: TkText no -textvariable workaround?
Date: Tue, 16 Jul 2002 23:53:28 +0900
Message-ID: 1026831519.651.19.camel@silver

more along these lines:

text = TkVariable.new
widget = TkEntry.new(parent) { textvariable text1 }

then later on i can just say:

text = ‘whatever’

and the widget will automatically be set to ‘whatever’. but TkText
doesn’t support textvariable. so how do i do the same thing with it?

Why are TkText#value and TkText#value= method
not enough in your purpose?

                              Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Peter Hickman screwed up:

The Perl/Tk pocket reference from O’Reilly is a usefull reference, if
somewhat terse. What you are seeing is more an artefact of Tk than Ruby.

Oops that should have been the Tcl/Tk pocket reference

The TkText widget however does not have this option, it makes little
sense to have text ‘This is the text’ so what you have is…
[snipped examples]

The reason is partly due to underlying Tk code and not something we could
just fix in Ruby (at least that’s my understanding and PH says as much
below-- please correct if wrong). More importantly TkText’s “value” is not
just text! I don’t see it mentioned elsewhere in the thread, so I thought
I better point it out.

TkText is a very powerful widget, including ability to insert pictures, use
multiple fonts, colors, etc, and other properties that would be completely
confounded by any attempt to tie the “value” to a string like TkLabel or
TkEntry can do. TkText is almost misleading, maybe should be TkRichText.
:slight_smile:

The Perl/Tk pocket reference from O’Reilly is a usefull reference, if
somewhat terse. What you are seeing is more an artefact of Tk than Ruby.

I have never been disappointed using man pages at http://www.tcl.tk or with
the sample scripts in RAA.

To Tom Sawyer’s original problem, you’ll have to bind to keystroke event or
some other widget event to capture changes as they happen, if that is your
intent.

sample code to demonstrate:
require 'tk’
r = TkRoot.new
t = TkText.new®.pack
a = ""
t.bind(‘Key’, proc{|x| a << x.keysym_num.chr })
r.bind(‘Button-1’, proc{puts a})
Tk.mainloop

Type into text block to generate a, press mouse button in text box to
print. On my machine this generates errors on certain keystrokes, like
Shift, because its keysym_num is out of range for chr – however capital
letters still work, just not smoothly. The proc{} could easily test for
legal values – and process things like delete or cursor movements, etc.

  • -michael

Michael_C_Libby{x(at)ichimunki(dot)com}__

···

On Tuesday 16 July 2002 10:15, Peter Hickman wrote:

      my website: http://www.ichimunki.com/           | 

____ public key at http://www.ichimunki.com/public.key ____|

my intention was to create a mixin module that by merely being mixed-in
would tie the object’s instance variables to a tk gui form. thus when a
variable changes the form changes and vice-versa, automatically
–without having to add any special code to the object itself.

···

On Tue, 2002-07-16 at 19:52, nagai@ai.kyutech.ac.jp wrote:

Why are TkText#value and TkText#value= method
not enough in your purpose?


~transami

(") dobee dobee do…
\v/
^ ^

Perl/Tk is also good. The Ruby binding closely parallels.

···

On Tuesday 16 July 2002 10:37 am, Peter Hickman wrote:

Peter Hickman screwed up:

The Perl/Tk pocket reference from O’Reilly is a usefull reference, if
somewhat terse. What you are seeing is more an artefact of Tk than Ruby.

Oops that should have been the Tcl/Tk pocket reference

/me sighs remembering when he did that with good ol’ Delphi

I’ve no experience of Tk, moreover I did not follow the thread from the
beginning, so apologies in advance. Does anything prevent you to extend
the TkText object with a new setter method that also triggers refresh?

class TkText
def my_value=(text)
# set the text
# trigger refresh
end
end

Anyway, a noble purpose, especially for people (like me!) not too fond
of writing GUI code. Let us (or the RAA) know about your results!

Massimiliano

···

On Wed, Jul 17, 2002 at 11:32:18AM +0900, Tom Sawyer wrote:

On Tue, 2002-07-16 at 19:52, nagai@ai.kyutech.ac.jp wrote:

Why are TkText#value and TkText#value= method
not enough in your purpose?

my intention was to create a mixin module that by merely being mixed-in
would tie the object’s instance variables to a tk gui form. thus when a
variable changes the form changes and vice-versa, automatically
–without having to add any special code to the object itself.

Thanks Massimiliano,

your solution may do the trick. i’ll check it out.

but sadly i must report my efforts are not going as well as i would have
hoped. Tk is a rather sad excuse for a gui toolkit, it seems. i would
rather blame myself, but after hours of trying to make a grided frame
expand to fill a resized window with out success, i am left feeling like
it’s just not worth it. but all is not lost, i will return to it after a
while and try again.

~transami

···

On Thu, 2002-07-18 at 03:49, Massimiliano Mirra wrote:

On Wed, Jul 17, 2002 at 11:32:18AM +0900, Tom Sawyer wrote:

On Tue, 2002-07-16 at 19:52, nagai@ai.kyutech.ac.jp wrote:

Why are TkText#value and TkText#value= method
not enough in your purpose?

my intention was to create a mixin module that by merely being mixed-in
would tie the object’s instance variables to a tk gui form. thus when a
variable changes the form changes and vice-versa, automatically
–without having to add any special code to the object itself.

/me sighs remembering when he did that with good ol’ Delphi

I’ve no experience of Tk, moreover I did not follow the thread from the
beginning, so apologies in advance. Does anything prevent you to extend
the TkText object with a new setter method that also triggers refresh?

class TkText
def my_value=(text)
# set the text
# trigger refresh
end
end

Anyway, a noble purpose, especially for people (like me!) not too fond
of writing GUI code. Let us (or the RAA) know about your results!

Massimiliano


~transami

(") dobee dobee do…
\v/
^ ^