TkEntry - how do I read/capture what was typed?

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

···

--
Posted via http://www.ruby-forum.com/.

Hi,

···

From: Joe Collins <joec_49@hotmail.com>
Subject: Re: TkEntry - how do I read/capture what was typed?
Date: Tue, 6 Sep 2011 21:38:21 +0900
Message-ID: <ea33915478df1eaeba86064efed3f378@ruby-forum.com>

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
-------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
Department of Artificial Intelligence, Kyushu Institute of Technology