I would like a working example please
I know how to create an TKentry field but I cannot figure out how to
capture what was typed…anybody have worked out examples?
You must associate a Tk variable object to your Entry field :
@myVar = TkVariable.new
@myEntryField.textvariable(@myVar)
…
@myEntry = @myVar.value
Examples of tk programming here :
http://www.ugcs.caltech.edu/~rise/tk-demos/
Hope this help.
below is the relevant section of my code
entry1 = TkEntry.new(root) {
}
entry2 = TkEntry.new(root) do
show ‘*’
end
variable1 = TkVariable.new
variable2 = TkVariable.new
entry1.textvariable = variable1
entry2.textvariable = variable2
variable1.value = “”
variable2.value = “Enter any confidential value”
@myVar = TkVariable.new
@entry1.textvariable(@myVar) #<=givbes undefined method
@myEntry = @myVar.value
entry1.place(‘height’ => 25,
‘width’ => 150,
‘x’ => 10,
‘y’ => 10)
entry2.place(‘height’ => 25,
‘width’ => 150,
‘x’ => 10,
‘y’ => 40)
Tk.mainloop
entry1.textvariable = variable1
entry1.place(‘height’ => 25,
‘width’ => 150,
‘x’ => 10,
‘y’ => 10)entry2.place(‘height’ => 25,
‘width’ => 150,
‘x’ => 10,
‘y’ => 40)
Tk.mainloop
@entry1 specifies an instance variable in a class and is not defined in
your code. Has probably the nil value.
I believe that it’s not a good design to process the value of an entry
field when it has just been typed (if possible with tk ?). Usually, you
grab the value when an OK button has been clicked.
Thanks Michel
can TKEntry invoke a defined procedure to grab that input when the user
is done typing?
Also, I tried this:
@myVar = TkVariable.new
@entry1.textvariable(@myVar)
@myEntry = @myVar.value
and it gave an undefined method error on entry1.textvariable(@myVar)
any ideas?
Joe
Hi,
From: Joe C. [email protected]
Subject: Re: TkEntry - how do I read/capture what was typed?
Date: Tue, 6 Sep 2011 21:38:21 +0900
Message-ID: [email protected]
can TKEntry invoke a defined procedure to grab that input when the user
is done typing?
How about this?
require ‘tk’
class PasswdEntry < TkFrame
def initialize(*args)
super(*args)
@frame = self
width = 15
# create widgets
@label1 = TkLabel.new(@frame, :text=>'Password ')
@var1 = TkVariable.new
@entry1 = TkEntry.new(@frame, :textvariable=>@var1,
:width=>width, :show=>'*',
:foreground=>'red')
@passwd_ok = false
@label2 = TkLabel.new(@frame, :text=>'Re-Type ')
@var2 = TkVariable.new
@entry2 = TkEntry.new(@frame, :textvariable=>@var2,
:width=>width, :show=>'*',
:foreground=>'red')
@retype_ok = false
# grid widgets
TkGrid.column(@frame, 0, :weight=>0)
TkGrid.column(@frame, 1, :weight=>1)
@label1.grid(:row=>0, :column=>0, :sticky=>'w')
@entry1.grid(:row=>0, :column=>1, :sticky=>'news')
@label2.grid(:row=>1, :column=>0, :sticky=>'w')
@entry2.grid(:row=>1, :column=>1, :sticky=>'news')
# trace variables
@var1.trace(:write){|var, idx, op| check_passwd(var.value)}
@var2.trace(:write){|var, idx, op| check_retype(var.value)}
# define bindings
@entry1.bind('Return'){@entry2.focus if @passwd_ok}
@entry2.bind('Return'){
if @cmd && @passwd_ok && @retype_ok
@cmd.call(@var1.value)
@entry1.focus
end
}
end
def callback(&block)
@cmd = block
end
def focus
@entry1.focus
end
def get_passwd
if @passwd_ok && @retype_ok
@var1.value
else
false
end
end
######################
private
######################
def check_passwd(str)
# check pattern of password string
#if str.length >= 6 && str =~ /\d/ && str =~ /[A-Z]/ && str =~
/[a-z]/
if str.length >= 6
@entry1.foreground ‘black’
@passwd_ok = true
else
@entry1.foreground ‘red’
@passwd_ok = false
end
end
def check_retype(str)
if @passwd_ok && str == @var1.value
@entry2.foreground ‘black’
@retype_ok = true
else
@entry2.foreground ‘red’
@retype_ok = false
end
end
end
passwd = PasswdEntry.new.pack
btn = TkButton.new(:text=>‘print passwd’,
:command=>proc{p passwd.get_passwd}).pack
passwd.callback{|str| btn.invoke}
passwd.focus
Tk.mainloop