开发者

How to select all text in a tk input box (ruby)

开发者 https://www.devze.com 2023-03-29 09:44 出处:网络
Just coming up to speed with tk / ruby. I have a very sim开发者_开发技巧ple window with a one line text input area, a \"go\" button, and a feedback label.

Just coming up to speed with tk / ruby.

I have a very sim开发者_开发技巧ple window with a one line text input area, a "go" button, and a feedback label.

The desired behavior is that a user enters some text, hits the "go" button, and the code does some work.

When its all over the input box should have the text entered, but it should be highlighted and the focus in the input box, so that if the user begins typing again what was in the box will be overwritten.

In .net this was pretty easy, just say .selectall to the input field. can't figure out how to do it in tk.

    root = TkRoot.new {title "Test App"}
content = Tk::Tile::Frame.new(root) {padding "3 3 12 12"}.grid( :sticky => 'nsew')
TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root, 0, :weight => 1
$job= TkVariable.new; $status = TkVariable.new
$j = Tk::Tile::Entry.new(content) {
      width 14; textvariable $job}.grid( :column => 1, :row => 1, :sticky => 'we' )
Tk::Tile::Button.new(content) {
         text 'Find Job'
         command {go_do_something}
       }.grid( :column => 2, :row => 1, :sticky => 'w')

    def go_do_something
       # ... do some processing, then I want to...
       # $j.focus
       # $j.select_all
    end

Thanks in advance


Looks like you need to add all of the widget's text to the special "sel" (selection) tag.

What your sw adds to the sel tag will become selected on the screen.

See docs


Order is important:

$j.focus; 
$j.selection_range(0,100)

This works to get the text box selected. Getting the focus back to the window is another question I will ask elsewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消