Wiki
Version 2 (modified by callisto, 12 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 Boxes

Radio Buttons

Combo Boxes

Spin Boxes

Attachments