Version 3 (modified by callisto, 13 years ago) |
---|
Gtk Simple Widgets
These are widgets that can be clicked or otherwise react to a user's request. Gtk provides many different types of widgets, and only a selection is illustrated here.
Buttons
A button displays some text and/or icon, and generates a 'clicked' signal when clicked by the user. The simplest way to create a button is to pass it some text to use as the label:
button1 = Button("Click me")
An alternative way is to use one of the stock actions (see here for a complete list). For example:
button1 = Button(Stock.quit)
To connect a method to a button, listen to the 'clicked' event. For example:
listen button1.clicked, ref .buttonClicked
Below is a complete program to create and pack two buttons into a window. When the top button is clicked, a message is printed on the terminal, and when the bottom button is clicked, the application quits. The window should look something like:
# @args -pkg:gtk-sharp-2.0 # remove initial '#' use Gtk class ExampleWindow inherits Window cue init base.init("Buttons Example") .setDefaultSize(300,200) listen .deleteEvent, ref .quit .createWidgets def quit(obj, e) Application.quit def createWidgets # Create a button with given title text button1 = Button("Click me") # associate action when clicked listen button1.clicked, ref .buttonClicked # Create a button using stock 'quit' label and icon button2 = Button(Stock.quit) # associate action when clicked listen button2.clicked, ref .quit # Add the two buttons to the window within a box box = VBox(true, 0) box.packStart(button1, false, false, 20) box.packStart(button2, false, false, 20) .add(box) def buttonClicked(obj, e) print "Clicked button" class MainProgram def main Application.init window = ExampleWindow() window.showAll Application.run
Check Buttons
A check button has a label and a tick box; clicking on the button will tick or untick the box. The checkbutton's state is stored in its attribute 'active'. An example of a check button:
(To use this code, replace the 'createWidgets' method in the above example, and add the field definition.)
var _checkbutton as CheckButton # store the check button as a field def createWidgets _checkbutton = CheckButton("select") # create the check button # Create a button to print out state of other widgets button = Button("Show State") # associate action when clicked listen button.clicked, ref .showState # Add the two buttons to the window within a box box = VBox(true, 0) box.packStart(_checkbutton, false, false, 20) box.packStart(button, false, false, 20) .add(box) def showState(obj, e) print "Check button state is", _checkbutton.active # report the check button's state
Radio Buttons
Combo Boxes
Spin Boxes
Attachments
- buttons.png (10.8 KB) - added by callisto 13 years ago.
- checkbutton.png (8.3 KB) - added by callisto 13 years ago.
- spinbutton.png (9.3 KB) - added by callisto 13 years ago.
- combobox.png (10.4 KB) - added by callisto 13 years ago.
- radiobuttons.png (12.4 KB) - added by callisto 13 years ago.