How to catch the indicator for a checkbutton

Hi all,

This post is kind of repeating my earlier on. I search the forum and
google for several days but I don’t get a clue for that. Here is my
code:

require ‘tk’

root=TkRoot.new()
root.title=“wwwcheckbutton”

$checkbutton1=TkCheckButton.new() do
text “check button 1”
command proc{puts variable.value}
pack()
end

Tk.mainloop()

When I run the code, I can see 0 if I uncheck the checkbutton or 1 if I
check it from console window. But I don’t know how to pass 0 or 1
outside the block.

So my question: how do I catch the indicator “check” or “uncheck”?
Specifically I want to catch this value 0 or 1 and based on 0 and 1 to
do other stuff: pseudocode—if the result is 0, which means the user
selects uncheck then I ask Ruby do job X; if the result is 1, which
means the user check it then I ask Ruby do job Y.

Thanks,

Hi all,

After try and error, here comes my solution for this topic: I have to
use a second widget to get the status of another one. And based on
this, it is my gut feeling that third puts() actually executes before
mainloop runs, which means the new status of button1 never get a chance
to pass out of widget. So the third puts() always print out 0. I am not
sure if my understanding is correct. But anyway I now check the status
of other widget and return their value when and where ever I want.

Any comments?

Thanks.
##################################################
require ‘tk’

root=TkRoot.new()
root.title=“wwwcheckbutton”

$checkbutton1=TkCheckButton.new() do
text “check button 1”
pack()
end

$checkbutton2=TkCheckButton.new() do
text “check button 2”
command proc {
if $checkbutton1[“variable”]==1
puts “Button 1 is pressed”
else
puts " Button 1 is NOT pressed"
end
}
pack()
end

puts “I get the status of button 1 outside widgets:”+
$checkbutton1[“variable”]###always puts()status of checkbutton1 to 0

Tk.mainloop()
#################################################

it is my gut feeling that third puts() actually executes
before mainloop runs,

Was it your belief that ruby shuffles all the statements in a program,
then executes them in random order? Otherwise, how did you think it was
possible for the third puts statement to execute after the mainloop
statement? Are you not aware that ruby executes the lines of code in a
program sequentially starting at the top of the file and working towards
the bottom, with def’s and proc’s creating blocks of code whose
execution is deferred until some future date?

Until mainloop() is called, no widget can respond to any events, and
therefore it is impossible for any code in a widget’s event handler to
execute before the code that comes before the mainloop statement.

But I don’t know how to pass 0 or 1
outside the block.

Why do you need to do that? The checkbutton always knows its
state.

So my question: how do I catch the indicator “check” or “uncheck”?

In a gui program, you write code to respond to events; if there are
no events, then nothing in your program will execute. If you
want to know whether a checkbutton is checked or not,
you have to decide at what event you want to query the checkbutton for
its state. The obvious choice is when the checkbutton is clicked. If
you want to get the checkbutton’s state at some later event, then what
event is that? After a certain time has passed(there are timers in Tk),
after another button is clicked? A submit button, perhaps? When the
user moves their mouse over a certain widget? What? Things in a
gui program only execute in response to events.

But I don’t know how to pass 0 or 1
outside the block.

Why do you need to do that? The checkbutton always knows its
state.

You can link a checkbutton’s value to a variable:

require ‘tk’

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

def cb1_onclick
puts ‘hello’
puts $my_var
end

def cb2_onclick
puts “$my_var: #{$my_var}”
end

$my_var = TkVariable.new 200
#If no value specified above, then checkbutton is in an
#indeterminate state(not checked, not unchecked), so
#tk displays a dash

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

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

Tk.mainloop

Thank you for the reply and detailed explanations. Now I have a better
understand on working GUI. Tk.mainloop is the blackbox where all widgets
communicate with each other and how things happen. Any codes before this
line help to set up the GUI, any codes after it are never seen by the
GUI.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs