using TkEntry with copy/paste

  • Thread starter Mathew Cucuzella
  • Start date
M

Mathew Cucuzella

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.


<code>
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
</code>

Thanks,
Mathew Cucuzella
 
H

Hidetoshi NAGAI

From: Mathew Cucuzella <[email protected]>
Subject: using TkEntry with copy/paste
Date: Mon, 7 Jun 2010 18:26:05 +0900
Message-ID: said:
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

<code>
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
</code>
 
M

Mathew Cucuzella

Hidetoshi said:
How about the following

<code>
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
</code>

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.

<code>
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"
}
}
</code>
 
H

Hidetoshi NAGAI

From: Mathew Cucuzella <[email protected]>
Subject: Re: using TkEntry with copy/paste
Date: Tue, 8 Jun 2010 01:33:41 +0900
Message-ID: said:
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top