From: Hidetoshi NAGAI Date: 2004-04-26T10:54:21+09:00 Subject: Re: tk: how to dynamically remove / add widgets? Hi, From: "Its Me" Subject: Re: tk: how to dynamically remove / add widgets? Date: Mon, 26 Apr 2004 00:09:06 +0900 Message-ID: > This seems to be about binding and re-binding the callbacks on a widget. > What I want to do is actually remove widgets and add new widget to some > containing frame. I intend to show that a callback for a widget can control another widget. In other words, you can pack/unpack some widgets in a callback procedure. If you hate the frame size is changed when packing some widgets in the frame, you must set TkPack.propagate to false. Please see the following sample script, and compare the difference of propagate mode (true/false). ----------------------------------------------------------- #!/usr/bin/env ruby require 'tk' TkLabel.new(:text=>"Please click the bottom frame").pack f = TkFrame.new(:width=>400, :height=>100, :background=>'yellow', :relief=>'ridge', :borderwidth=>5).pack TkPack.propagate(f, false) # <== important!! list = (1..3).collect{|n| TkButton.new(f, :text=>"button-#{'X'*n}"){ command proc{ puts "button-#{'X'*n}" self.unpack } } } f.bind('1', proc{ w = list.shift w.unpack list.push(w) list[0].pack(:anchor=>:center) }) Tk.mainloop ----------------------------------------------------------- -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)