From: Hidetoshi NAGAI Date: 2004-04-25T16:17:12+09:00 Subject: Re: tk: how to dynamically remove / add widgets? Hi, From: "Its Me" Subject: tk: how to dynamically remove / add widgets? Date: Sun, 25 Apr 2004 06:04:06 +0900 Message-ID: > On a certain event I want to modify the frame > e.g. remove the widget @a, add another widget @b. Use 'bind' method. The follwing is a sample of binding. # It will be included in the sample directry of the renewal # version of Ruby/Tk. -------------------------------------------------------- #!/usr/bin/env ruby require 'tk' class Button_clone < TkLabel def initialize(*args) @command = nil if args[-1].kind_of?(Hash) keys = _symbolkey2str(args.pop) @command = keys.delete('command') args.push(keys) end super(*args) self.configure(:highlightthickness=>1, :padx=>'3m', :pady=>'1m', :relief=>'raised') self.bind('Enter', proc{self.background(self.activebackground)}) self.bind('Leave', proc{ self.background(self.highlightbackground) self.relief('raised') }) self.bind('ButtonPress-1', proc{self.relief('sunken')}) self.bind('ButtonRelease-1', proc{ self.relief('raised') @command.call if @command }) end def command(cmd = Proc.new) @command = cmd end end TkLabel.new(:text=><'red').pack(:pady=>3) v = TkVariable.new(0) TkFrame.new{|f| TkLabel.new(f, :text=>'click count : ').pack(:side=>:left) TkLabel.new(f, :textvariable=>v).pack(:side=>:left) }.pack TkButton.new(:text=>'normal Button widget', :command=>proc{ puts 'button is clicked!!' lbl.text 'button is clicked!!' v.numeric += 1 }){ pack(:fill=>:x, :expand=>true) } Button_clone.new(:text=>'Label with Button binding', :command=>proc{ puts 'label is clicked!!' lbl.text 'label is clicked!!' v.numeric += 1 }){ pack(:fill=>:x, :expand=>true) } Tk.mainloop -------------------------------------------------------- -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)