Forum: Ruby Ruby/TK packForget method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex D. (Guest)
on 2007-03-05 17:24
Is there a Ruby/TK equivalent to the Perl/TK packForget or gridForget
methods, which will remove a widget from display, but doesn't destroy
the widget?
Morton G. (Guest)
on 2007-03-06 06:10
(Received via mailing list)
On Mar 5, 2007, at 10:24 AM, Alex DeCaria wrote:

> Is there a Ruby/TK equivalent to the Perl/TK packForget or gridForget
> methods, which will remove a widget from display, but doesn't destroy
> the widget?

The following example shows how 'TkGrid.forget' can cause some
buttons to disappear.

<code>
# Clicking on the "four" button causes the "four" and "five" buttons to
# disappear.

require 'tk'

DEBUG = []

begin
    root = TkRoot.new {title 'Tk Button Grid'}

    lbls = ["one", "two", "three", "four", "five"]
    btns = lbls.collect do |lbl|
       TkButton.new(root, "text"=> lbl, "command"=>lambda { Tk.bell })
    end
    btns[0, 3].each_with_index do |b, i|
       b.grid("row"=>0, "column"=>2*i, "columnspan"=>2)
    end
    btns[3, 2].each_with_index do |b, i|
       b.grid("row"=>1, "column"=>3*i, "columnspan"=>3, "sticky"=>"ew")
    end
    btns[3].command = lambda { TkGrid.forget(*btns[3, 2]) } # <= *******

    quit_btn = TkButton.new(root) {
       text "Exit"
       command { root.destroy }
    }
    quit_btn.grid("row"=>2, "column"=>2, "columnspan"=>2,
"sticky"=>"ew")

    # Set window geometry.
    min_w, min_h = 200, 95
    root.minsize(min_w, min_h)
    # Initial placement is top-center of screen.
    root_x = (root.winfo_screenwidth - min_w) / 2
    root.geometry("#{min_w}x#{min_h}+#{root_x}+50")

    # Set resize permissions.
    root.resizable(false, false)

    # Make Cmnd+Q work as expected on OS X.
    root.bind('Command-q') { Tk.root.destroy }

    Tk.mainloop
ensure
    puts DEBUG unless DEBUG.empty?
end
</code>

I imagine 'TkPack.forget' will work for packed widgets, but I have
not actually tried it.

Regards, Morton
This topic is locked and can not be replied to.