Forum: Ruby how to uncheck a checkbutton

73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2014-06-15 02:21
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"

}
#############################################
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2014-06-15 18:33
> 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.
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2014-06-15 19:18
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.