In addition to setting a widget's options when it's created, you can
reconfigure a widget while it's running. Every widget supports the
configure
method, which takes a
Hash
or a code block in the
same manner as
new
. We can modify the first example to change
the label text in response to a button press:
lbl = TkLabel.new(top) { justify 'center'
text 'Hello, World!';
pack('padx'=>5, 'pady'=>5, 'side' => 'top') }
TkButton.new(top) {
text "Cancel"
command proc { lbl.configure('text'=>"Goodbye, Cruel World!") }
pack('side'=>'right', 'padx'=>10, 'pady'=>10)
}
|
Now when the
Cancel
button is pressed, the text in the label will
change immediately from ``
Hello, World!
'' to ``
Goodbye, Cruel
World!
''
You can also query widgets for particular option values using
cget
:
require 'tk'
|
b = TkButton.new {
|
text "OK"
|
justify 'left'
|
border 5
|
}
|
b.cget('text')
|
� |
"OK"
|
b.cget('justify')
|
� |
"left"
|
b.cget('border')
|
� |
5
|