Using TkEntry with copy/paste

I’m writing a Ruby/Tk program where I want a TkEntry field with a
state of “readonly” so a user can copy/paste from it, but not modify
it. Unfortunately this prevents me from programatically changing it,
which I want to do. Is there a better way to accomplish that?

Sample code: TkEntry does not display “hi” after button click.

require 'tk' top = TkRoot.new entry = TkEntry.new(top) {state "readonly";grid('row'=>0, 'column'=>0)}

TkButton.new(top) {text ‘Convert’; grid(‘row’=>1, ‘column’=>0);

the “entry” field update fails, as documented

command proc {entry.value = “hi”}
}
Tk.mainloop

Environment

Ubuntu 10.4

2.6.32-22-generic #36-Ubuntu SMP Thu Jun 3 22:02:19 UTC 2010

i686 GNU/Linux

ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]

libtcltk-ruby

libtcltk-ruby1.8

Thanks,
Mathew C.

From: Mathew C. [email protected]
Subject: using TkEntry with copy/paste
Date: Mon, 7 Jun 2010 18:26:05 +0900
Message-ID: [email protected]

I’m writing a Ruby/Tk program where I want a TkEntry field with a
state of “readonly” so a user can copy/paste from it, but not modify
it. Unfortunately this prevents me from programatically changing it,
which I want to do. Is there a better way to accomplish that?

How about the following

require 'tk' top = TkRoot.new var = TkVariable.new entry = TkEntry.new(top, :state=>'readonly', :textvariable=>var).grid(:row=>0, :column=>0) TkButton.new(top, :text=>'Convert', :command=>proc{var.value = 'hi'}).grid(:row=>1, :column=>0)

Tk.mainloop

Hidetoshi NAGAI wrote:

How about the following

require 'tk' top = TkRoot.new var = TkVariable.new entry = TkEntry.new(top, :state=>'readonly', :textvariable=>var).grid(:row=>0, :column=>0) TkButton.new(top, :text=>'Convert', :command=>proc{var.value = 'hi'}).grid(:row=>1, :column=>0)

Tk.mainloop

Thanks, that will work. I tried what I thought was the equivalent
(below) but it didn’t work. The only real difference was that I
didn’t pass my config params as function arguments.

require 'tk'

top = TkRoot.new

variable used in entry field, written with button press

text_var = TkVariable.new
entry = TkEntry.new(top) {textvariable = text_var; grid(‘row’=>0,
‘column’=>0)}

TkButton.new(top) {text ‘Convert’; grid(‘row’=>1, ‘column’=>0);
command proc {
# the next line does not show the string in the TkEntry
text_var.value = “using variable”
}
}

Hidetoshi NAGAI wrote:

You define a local variable.
Please use “textvariable text_var” or “self.textvariable = text_var”.

Thanks, that fixed it.

From: Mathew C. [email protected]
Subject: Re: using TkEntry with copy/paste
Date: Tue, 8 Jun 2010 01:33:41 +0900
Message-ID: [email protected]

Thanks, that will work. I tried what I thought was the equivalent
(below) but it didn’t work. The only real difference was that I
didn’t pass my config params as function arguments.
(snip)
entry = TkEntry.new(top) {textvariable = text_var; grid(‘row’=>0,
^^^^^^^^^^^^^^^^^^^^^^^
You define a local variable.
Please use “textvariable text_var” or “self.textvariable = text_var”.