# How can i 'Clear' a TkText...box?

**URL:** <https://www.ruby-forum.com/t/how-can-i-clear-a-tktext-box/118425>\
**Category:** Ruby\
**Created:** [October 26, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/how-can-i-clear-a-tktext-box/118425 "2007-10-26T19:55:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 26, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/how-can-i-clear-a-tktext-box/118425/1 "2007-10-26T19:55:38Z")

</div>

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
`

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 27, 2007, 4:29am UTC](https://www.ruby-forum.com/t/how-can-i-clear-a-tktext-box/118425/2 "2007-10-27T04:29:44Z")

</div>

On Oct 26, 2007, at 1:55 PM, removed\_email\_address@domain.invalid wrote:

> #def delete(first, last)  
> ‘pady’ =\> 10)  
> text “Clear”  
> background “white”  
> }.pack(‘side’ =\> ‘right’, ‘fill’ =\> ‘both’, ‘expand’ =\> true,  
> 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
