| 1 | = Gtk Simple Widgets = |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | == Buttons == |
| 6 | |
| 7 | A button displays some text and/or icon, and generates a 'clicked' signal when clicked by the user. |
| 8 | The simplest way to create a button is to pass it some text to use as the label: |
| 9 | |
| 10 | {{{ |
| 11 | #!cobra |
| 12 | button1 = Button("Click me") |
| 13 | }}} |
| 14 | |
| 15 | An alternative way is to use one of the stock actions (see [http://developer.gnome.org/gtk/2.24/gtk-Stock-Items.html#GTK-STOCK-OK:CAPS here] for a complete list). For example: |
| 16 | |
| 17 | {{{ |
| 18 | #!cobra |
| 19 | button1 = Button(Stock.quit) |
| 20 | }}} |
| 21 | |
| 22 | To connect a method to a button, listen to the 'clicked' event. For example: |
| 23 | |
| 24 | {{{ |
| 25 | #!cobra |
| 26 | listen button1.clicked, ref .buttonClicked |
| 27 | }}} |
| 28 | |
| 29 | The code below is a complete example showing how to create and pack to buttons into a window. The final window should look something like: |
| 30 | |
| 31 | [[Image(buttons.png)]] |
| 32 | |
| 33 | {{{ |
| 34 | #!cobra |
| 35 | # @args -pkg:gtk-sharp-2.0 # remove initial '#' |
| 36 | |
| 37 | use Gtk |
| 38 | |
| 39 | class ExampleWindow inherits Window |
| 40 | cue init |
| 41 | base.init("Buttons Example") |
| 42 | .setDefaultSize(300,200) |
| 43 | listen .deleteEvent, ref .quit |
| 44 | .createWidgets |
| 45 | |
| 46 | def quit(obj, e) |
| 47 | Application.quit |
| 48 | |
| 49 | def createWidgets |
| 50 | # Create a button with given title text |
| 51 | button1 = Button("Click me") |
| 52 | # associate action when clicked |
| 53 | listen button1.clicked, ref .buttonClicked |
| 54 | |
| 55 | # Create a button using stock 'quit' label and icon |
| 56 | button2 = Button(Stock.quit) |
| 57 | # associate action when clicked |
| 58 | listen button2.clicked, ref .quit |
| 59 | |
| 60 | # Add the two buttons to the window within a box |
| 61 | box = VBox(true, 0) |
| 62 | box.packStart(button1, false, false, 20) |
| 63 | box.packStart(button2, false, false, 20) |
| 64 | |
| 65 | .add(box) |
| 66 | |
| 67 | def buttonClicked(obj, e) |
| 68 | print "Clicked button" |
| 69 | |
| 70 | class MainProgram |
| 71 | def main |
| 72 | Application.init |
| 73 | window = ExampleWindow() |
| 74 | window.showAll |
| 75 | Application.run |
| 76 | }}} |
| 77 | |
| 78 | == Check Boxes == |
| 79 | |
| 80 | |
| 81 | == Radio Buttons == |
| 82 | |
| 83 | |
| 84 | == Combo Boxes == |
| 85 | |
| 86 | |
| 87 | == Spin Boxes == |
| 88 | |