How to uncheck a checkbutton

Hi all,

I write a small GUI with a checkbutton using Tk. The default setting is
to “check or select” the checkbutton. But I don’t know the codes to
uncheck/deselect it from user end. I wonder if anyone could please help
me. Thanks.

Here is part of my code:
############################################
$checkbutton_speech=TkCheckButton.new() do
text “Speech”
font TkFont.new(“times 12 bold”)
place(:x=>550,:y=>613)
command (select)
end

$checkbutton_speech.bind(“ButtonPress”){
$checkbutton_speech[:command]=“deselect”

}
#############################################

But I don’t know the codes to
uncheck/deselect it from user end

The user end? The user clicks on the button to check it or uncheck it.
The user doesn’t use codes.

If the programmer wants to display the checkbutton as checked or
unchecked:

  1. Associate the checkbutton with a TkVariable.
  2. Specify an onvalue and an offvalue for the TKCheckButton.
  3. Set the TKVariable to either the onvalue or the offvalue to display
    the checkbutton as checked or unchecked.

require ‘tk’

root = TkRoot.new
root.title = “MyWindow”

def cb1_onclick
puts ‘hello’
puts $my_var
end

$my_var = TkVariable.new 200

cb1 = TkCheckButton.new(root) do
text “I am checkbutton1”
background “red”
height 2
width 2
place(‘height’ => 25,‘width’ => 150, ‘x’ => 10, ‘y’=> 10)
command “cb1_onclick”
variable $my_var
onvalue 100
offvalue 200
end

Tk.mainloop

The default setting is
to “check or select” the checkbutton.

That’s not what I’m seeing. If I run the following program:

require ‘tk’

root = TkRoot.new
root.title = “MyWindow”

cb1 = TkCheckButton.new(root) do
text “I am checkbutton1”
background “red”
height 2
width 2
place(‘height’ => 25,‘width’ => 150, ‘x’ => 10, ‘y’=> 10)
end

Tk.mainloop

…the checkbutton is displayed unchecked, so it seems the default is
unchecked.

Hi 7stud,

Thank you very much for showing me how to use variable in Tk.

I try the first part of the codes. I think they show me the way I want
in my GUI.

As for second part of the code, you miss this line:

command (select)

If you add this line in your second part of the code, the default will
be selected.