Base Template

When creating new programs, I either use existing ones or when starting from scratch, a base template.

The following is the base template of writing a Seed program using GTK as the Graphical User interface. The code is the minimum required for a GTK application to work.

   1 #!/usr/bin/env seed
   2 
   3 //base.js
   4 
   5 Gtk = imports.gi.Gtk;
   6 Gtk.init(null, null);
   7 
   8 var window = new Gtk.Window();
   9 
  10 window.signal.hide.connect(Gtk.main_quit);
  11 
  12 window.show_all();
  13 
  14 Gtk.main();

When the program is run, an empty window is produced.

A breakdown of the program is as follows:

var Gtk = imports.gi.Gtk;
Gtk.init(null, null);

This imports the GTK library and initialises the object for use.

var window = new Gtk.Window();

This creates a variable called window that is set to gtk.Window. gtk.Window is the top level window which is used in most programs that have a GUI. There are others like gtk.Dialog which creates a dialog.

window.signal.hide.connect(Gtk.main_quit);

This connects the hide signal to a callback, in this case the function Gtk.main_quit which terminates the program, to the window. The hide signal is emitted when when the destroy button (usually x icon on top right hand side of title decoration is clicked on) or Alt + F4 is pressed.

Signals and callbacks will be explained in later post.

window.show_all();

This 'shows' the window on the screen. This command is required when the program is first run so that you can see the window but will be used later on if you were to hide the window for some reason, say to display a prompt dialog, then to reshow it.

Gtk.main();

This ‘runs’ the window that was just created. It creates an infinite loop until something is done i.e. the close button is clicked on the window or it is terminated by user via the signal created before this line.

Projects/Seed/Tutorial/Gtk/Base (last edited 2013-11-22 19:19:57 by WilliamJonMcCann)