在 Ruby 2.3.3 中查找哪个小部件具有焦点
Find which widget has focus in Ruby 2.3.3
在Python中,我使用:
who = widget.focus_get()
在 Perl 中:
$who = $widget->focusCurrent;
告诉我哪个小部件有焦点。所以:
- Ruby 在 Linux 下的等效代码是什么?
- 有没有关于底层RubyTk的好书或文章?我看到的所有文章都只是简单的内容。
[T]o tell me which widget has the focus[...w]hat is the equivalent code in Ruby under 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)对我有用。
Is there a good book or article about low-level Ruby Tk? All the articles I have seen only cover the simplistic stuff.
据我所知,没有人对 Ruby 的 Tk 包装器进行过详尽的描述(至少,不是英文)。但是,我们有包装器的
RDoc
文档,我们可以阅读描述如何使用其他语言的 Tk 的书籍。我认为最好的(出于 Ruby 目的)是 Nancy Walsh (1999) 的 Learning Perk/Tk。这是它的链接
Amazon,
Alibris
和
O'Reilly (along with its example code).
我们也可以 grep
包装器的库源代码(因为它安装在我们的计算机上)。
对常量 TkToplevel
(例如)执行此操作会导致文件 /Ruby/lib/ruby/2.2.0/tk/toplevel.rb
(或该文件驻留在您系统上的任何位置)。
在该文件中,搜索(搜索字符串 TkToplevel
)显示常量似乎被定义为 class Tk::Toplevel
的别名,已记录在案
here.
最终,研究如何使用 Tk 在 Ruby 中做不寻常的事情需要相当困难的努力。
通过在
Ruby forum.
TkDocs
教程非常有用,还有 Tcl 语言
Tk commands
参考文档。
在Python中,我使用:
who = widget.focus_get()
在 Perl 中:
$who = $widget->focusCurrent;
告诉我哪个小部件有焦点。所以:
- Ruby 在 Linux 下的等效代码是什么?
- 有没有关于底层RubyTk的好书或文章?我看到的所有文章都只是简单的内容。
[T]o tell me which widget has the focus[...w]hat is the equivalent code in Ruby under 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)对我有用。
Is there a good book or article about low-level Ruby Tk? All the articles I have seen only cover the simplistic stuff.
据我所知,没有人对 Ruby 的 Tk 包装器进行过详尽的描述(至少,不是英文)。但是,我们有包装器的 RDoc 文档,我们可以阅读描述如何使用其他语言的 Tk 的书籍。我认为最好的(出于 Ruby 目的)是 Nancy Walsh (1999) 的 Learning Perk/Tk。这是它的链接 Amazon, Alibris 和 O'Reilly (along with its example code).
我们也可以 grep
包装器的库源代码(因为它安装在我们的计算机上)。
对常量 TkToplevel
(例如)执行此操作会导致文件 /Ruby/lib/ruby/2.2.0/tk/toplevel.rb
(或该文件驻留在您系统上的任何位置)。
在该文件中,搜索(搜索字符串 TkToplevel
)显示常量似乎被定义为 class Tk::Toplevel
的别名,已记录在案
here.
最终,研究如何使用 Tk 在 Ruby 中做不寻常的事情需要相当困难的努力。
通过在 Ruby forum.
TkDocs 教程非常有用,还有 Tcl 语言 Tk commands 参考文档。