在Ruby 2.3.3中查找哪个小部件具有焦点

问题描述 投票:0回答:1

在Python中,我使用:

who = widget.focus_get()

在Perl中:

$who = $widget->focusCurrent;

告诉我哪个小部件有焦点。所以:

  1. Linux下的Ruby中的等效代码是什么?
  2. 是否有关于低级Ruby Tk的好书或文章?我所看到的所有文章只涵盖了简单的东西。
ruby tk
1个回答
1
投票

[T] o告诉我哪个小部件有焦点[...]帽子是Linux下Linux中的等效代码?

相当如此,如果你有三个Tk对象(例如),那么你可以说(在Ruby中):

array = [tk_object_1, tk_object_2, tk_object_3]
path = Tk.focus.path
index = array.find_index {|e| e.path == path}
object = array.at index

编辑:或者,更简单:

object = Tk.focus

以下是如何确定(多个Tk对象中的)对象:

array = [tk_object_1, tk_object_2, tk_object_3]
index = array.find_index {|e| e == object}

(编辑结束。)

我设法找到focus方法(在Ruby wrapper和原始Tcl)和path方法(在Ruby wrapper)的文档。

我写了一个演示设置并获得焦点的程序:

require 'tk'

def focus_array
  @focus_array ||= [
      f_content, e_content,
      to_one,    e_one,
      to_two,    e_two,
      ]
end

def focus_array_class_width_max
  @focus_array_class_width_max ||= focus_array.map {|e| e.class.to_s.length}.max
end

def focus_delay
  milliseconds = 1000
  Tk.after milliseconds, lambda_focus_rotate
  nil
end

def focus_force(object)
  force = true
  Tk.focus_to object, force
  nil
end

def focus_print
  path = Tk.focus.path
  index = focus_array.find_index {|e| e.path == path}
  s = klass_justified index
  puts "Item #{index}'s class and path are:  #{s}  #{path}"
  nil
end

def focus_rotate
  @counter += 1
  index = @counter % focus_array.length
  puts '' if 0 == index
  focus_force focus_array.at index
  nil
end

def klass_justified(index)
  focus_array.at(index).class.to_s.ljust focus_array_class_width_max
end

def lambda_focus_rotate
  @lambda_focus_rotate ||= Kernel.lambda do
    focus_rotate
    focus_print
    focus_delay
  end
end

def main
  root.title = 'Root'
  objects_create
  @counter = -1 # Start before first.
  focus_delay
  Tk.mainloop
  nil
end

def objects_create
# Keep order:
  e_content
  e_one
  e_two
  nil
end

def spacing_set(object)
  object.width 7
  object.grid padx: 40
end

#-------------
# Tk objects:

def e_content
  @e_content ||= begin
    e = Tk::Tile::Entry.new f_content
    spacing_set e
  end
end

def e_one
  @e_one ||= begin
    e = Tk::Tile::Entry.new to_one
    spacing_set e
  end
end

def e_two
  @e_two ||= begin
    e = Tk::Tile::Entry.new to_two
    spacing_set e
  end
end

def f_content
  $f_content ||= begin
    f = Tk::Tile::Frame.new root
    f.grid
  end
end

def root
  $root ||= TkRoot.new
end

def to_one
  @to_one ||= TkToplevel.new f_content
end

def to_two
  @to_two ||= TkToplevel.new f_content
end

main

它适用于我在Windows 7上使用Ruby 2.2.5(使用Tk 8.5.12)。

是否有关于低级Ruby Tk的好书或文章?我所看到的所有文章只涵盖了简单的东西。

据我所知,没有人写过关于Ruby的Tk包装器的详尽描述(至少,不是英文)。但是,我们有包装器的RDoc文档,我们可以阅读描述如何使用其他语言的Tk的书籍。我认为最好的(用于Ruby目的)是Nancy Walsh(1999)的Learning Perk / Tk。以下是AmazonAlibrisO'Reilly(以及它的example code)的链接。

我们也可以grep包装器的库源代码(因为它安装在我们的计算机上)。

对常量TkToplevel(例如)执行此操作会导致文件/Ruby/lib/ruby/2.2.0/tk/toplevel.rb(或该文件驻留在系统中的任何位置)。

在该文件中,搜索(对于字符串TkToplevel)显示该常量似乎被定义为类Tk::Toplevel的别名,其记录为here

最终,需要花费相当多的努力来研究如何使用Tk在Ruby中执行不寻常的事情。

通过询问Ruby forum已经获得了关于特定问题的良好结果。

TkDocs教程非常有用,还有Tcl语言的Tk commands参考文档。

© www.soinside.com 2019 - 2024. All rights reserved.