Question

Does Ruby/TK has any method to get parent of a widget like scrollbar.parent ?

Below code is working, but I want to set $log as @log(because this will be better structure when the view become complicate), but scrollbar need to refer to this

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
Was it helpful?

Solution 2

Redefined method 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

OTHER TIPS

Actually it is possible.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top