From: Morton Goldberg Date: 2007-10-27T11:29:12+09:00 Subject: Re: How can i 'Clear' a TkText...box??? On Oct 26, 2007, at 1:55 PM, wiz_pendases@yahoo.com wrote: > This is what i got ....i need to clear the big text box, when the user > hits the 'clear' button. > > > > irb > > # Start up Tk with Ruby > require 'tk' > > #def delete(first, last) > # @text.delete(0, end) > #end > > #def initialize > > # Main Window > root = TkRoot.new {title "Homework #4"} > > # Frames > left_frame = TkFrame.new(root).pack('side' => 'left', 'padx' => 10, > 'pady' => 10) > > right_frame = TkFrame.new(root).pack('side' => 'right', 'padx' => 10, > 'pady' => 10, 'fill' => 'both', 'expand' => true) > top_right_frame = TkFrame.new(right_frame).pack('side' => 'top', > 'fill' => 'both', 'expand' => true) > bottom_right_frame = TkFrame.new(right_frame).pack('side' => 'left', > 'fill' => 'both', 'expand' => true) > > # Clear and Exit buttons > TkButton.new(left_frame) do > text "Clear" > #command {delete(0, end)} > width 5 > background "white" > grid('column' => '0', 'row' => '0') > end > > TkButton.new(left_frame) do > text "Cancel" > command { exit } > width 5 > background "white" > grid('column' => '0', 'row' => '1') > end > > # Text Window > @text = TkVariable.new() > @text = " " > TkText.new(top_right_frame) { > background "white" > width 8 > borderwidth 2 > }.pack('side' => 'right', 'fill' => 'both', 'expand' => true, > 'ipady' => 20, 'ipadx' => 20) > > # Text entry window > TkLabel.new(bottom_right_frame) { text 'File:' }.pack('side' => > 'left') > TkEntry.new(bottom_right_frame){ > background "white" > }.pack('side' => 'left') > > > Tk.mainloop > require 'tk' # Main Window root = TkRoot.new {title "Homework #4"} # Frames left_frame = TkFrame.new(root).pack('side' => 'left', 'padx' => 10, 'pady' => 10) right_frame = TkFrame.new(root).pack('side' => 'right', 'padx' => 10, 'pady' => 10, 'fill' => 'both', 'expand' => true) top_right_frame = TkFrame.new(right_frame).pack('side' => 'top', 'fill' => 'both', 'expand' => true) bottom_right_frame = TkFrame.new(right_frame).pack('side' => 'left', 'fill' => 'both', 'expand' => true) # Text Window @text = TkVariable.new() @text = " " TkText.new(top_right_frame) { background "white" width 8 borderwidth 2 }.pack('side' => 'right', 'fill' => 'both', 'expand' => true, 'ipady' => 20, 'ipadx' => 20) # Text entry window TkLabel.new(bottom_right_frame) { text 'File:' }.pack('side' => 'left') entry_widget = TkEntry.new(bottom_right_frame) { background "white" }.pack('side' => 'left') # Clear and Exit buttons TkButton.new(left_frame) do text "Clear" command { entry_widget.delete(0, :end) } width 5 background "white" grid('column' => '0', 'row' => '0') end TkButton.new(left_frame) do text "Cancel" command { exit } width 5 background "white" grid('column' => '0', 'row' => '1') end Tk.mainloop The change in the order in which the widgets were created is important. The local entry_widget must exist before in can be used in the Clear button's command block. Regards, Morton