Ruby / TK是否有任何方法可以获取像scrollbar.parent
这样的小部件的父级?
下面的代码工作正常,但我想将$log
设置为@log
(因为当视图变得复杂时这将是更好的结构),但滚动条需要引用此
require 'tk'
$root = TkRoot.new do
# I want to set it as @log, but scrollbar need to refer to this
$log = TkText.new(self) do
class << self
attr_accessor :scrollbar
end
wrap 'none'
state 'disabled'
pack side: 'top',
padx: 5,
pady: 5,
fill: 'both',
expand: true
@scrollbar = TkScrollbar.new(self) do
pack side: 'right',
fill: 'y'
command do |*arg|
###### how to get parent without using $log
$log.yview *arg
end
end
yscrollcommand do |first, last|
@scrollbar.set first, last
end
end
end
Tk.mainloop
答案 0 :(得分:1)
实际上有可能。
require 'tk'
root = TkRoot.new
a = TkFrame.new.pack
b = TkFrame.new(a).pack
c = TkWinfo.parent(b)
puts a == c
puts b == c
Tk.mainloop
答案 1 :(得分:0)
重新定义的方法initialize
class TkWindow
attr_accessor :parent
alias _initialize initialize
def initialize(parent=nil, keys=nil)
@parent = parent if !parent.is_a?(Hash)
_initialize(parent, keys)
end
end