Wiki

Gtk Simple Window

The simplest Gtk program displays a window which closes when told to.

The following program is in two parts:

  1. The class MyWindow will be the main window for the application. MyWindow inherits from the Gtk Window class, and the init method initialises its parent with the given title.
    • .setDefaultSize is called to create a sensible size for the window
    • listen is used to attach a method to the deleteEvent, called when the user clicks the close window button.
  1. The main method in MainProgram goes through four stages to create and display a Gtk application:
    1. start the graphical application
    2. create an instance of the main window
    3. request all widgets in the main window to show themselves
    4. finally, run the graphical application
# @args -pkg:gtk-sharp-2.0  # remove initial '#'

use Gtk

class MyWindow inherits Window
    
    def onDeleteEvent(obj, args as DeleteEventArgs)
        """ 
        This method is tied to the deleteEvent, and is called when the close 
        button is clicked.  It causes the application to quit.
        """
        Application.quit

    cue init(title as String)
        # pass the title to the parent window
        base.init(title) 
        # set a size for our window
        .setDefaultSize(300, 200) 
        # register method to handle the close event
        listen .deleteEvent, ref .onDeleteEvent 

class MainProgram
    def main
        # initialise the application
        Application.init
        # create an instance of our main window
        window = MyWindow("Cobra's First Window")
        # show all the widgets within the window
        window.showAll
        # start running the application
        Application.run

Attachments