Ruby/Gtk Tutorial | ||
---|---|---|
<<< Previous | The Button Widget | Next >>> |
Toggle buttons are derived from normal buttons and are very similar, except they will always be in one of two states, alternated by a click. They may be depressed, and when you click again, they will pop back up. Click again, and they will pop back down.
Toggle buttons are the basis for check buttons and radio buttons, as such, many of the calls used for toggle buttons are inherited by radio and check buttons. These will be pointed out we come to them.
You create a new toggle button with toggle = Gtk::ToggleButton.new "Label"
As you can imagine, these work identically to the normal button widget calls. As before, you may omit the label and then pack a box inside the button.
To retrieve the state of the toggle widget, including radio and check buttons, we use a construct as shown in our example below. This tests the state of the toggle button. The signal of interest to us emitted by toggle buttons (the toggle button, check button, and radio button widgets) is the "toggled" signal. To check the state of these buttons, set up a signal handler to catch the toggled signal, and access the structure to determine its state. The callback will look something like:
def toggle_callback(bttn) if bttn.active? puts "Active" else puts "Not active" end end #... toggle = Gtk::ToggleButton.new "Button" toggle.signal_connect('toggled') {callback(toggle)} |
To force the state of a toggle button, use the method toggle.set_active(active) where active is true or false, corresponding to pressed and released. The radio and check buttons are children of the toggle button, and so they inherit this method.
Note that when you use the toggle.set_active method, and the state is actually changed, it causes the "clicked" and "toggled" signals to be emitted from the button.
<<< Previous | Home | Next >>> |
The Button Widget | Up | Check Buttons |