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

W

wiz_pendases

This is what i got ....i need to clear the big text box, when the user
hits the 'clear' button.

<code>

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
</code>
 
M

Morton Goldberg

This is what i got ....i need to clear the big text box, when the user
hits the 'clear' button.

<code>

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
</code>

<code>
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
</code>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top