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.
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)
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
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”
}
}
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”.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.