We can get information back from widgets with callbacks and by
binding variables.
Callbacks are very easy to set up. The
command
option (shown in
the
TkButton
call in the example that follows) takes a
Proc
object, which will be called when the callback fires. Here we use
Kernel::proc
to convert the
{exit}
block to a
Proc
.
TkButton.new(bottom) {
text "Ok"
command proc { p mycheck.value; exit }
pack('side'=>'left', 'padx'=>10, 'pady'=>10)
}
|
We can also bind a Ruby variable to a Tk widget's value using a
TkVariable
proxy. We show this in the following example. Notice
how the
TkCheckButton
is set up; the documentation says that the
variable
option takes a
var reference as an argument. For
this, we create a Tk variable reference using
TkVariable.new
.
Accessing
mycheck.value
will return the string ``
0
'' or
``
1
'' depending on whether the checkbox is checked. You can use
the same mechanism for anything that supports a var reference, such as
radio buttons and text fields.
mycheck = TkVariable.new
TkCheckButton.new(top) {
variable mycheck
pack('padx'=>5, 'pady'=>5, 'side' => 'left')
}
|