[clue-tech] First example Ruby Gtk program

Match Grun match at dim.com
Mon Oct 29 00:27:05 MDT 2007


On Thu, 25 Oct 2007 22:16:33 -0600
Dennis J Perkins <dennisjperkins at comcast.net> wrote:

> #! /usr/bin/env ruby
> 
> # Display the canonical "hello world" message of a first
> # program in a new programming language.
> 
> # Note that this program has a flaw that manifests itself 
> # when shutting down.  Clicking on the close button makes
> # the window disappear, but the event loop is still running.
> # This solution to this issue will be shown in another example
> # program.
> 
> # Use Ruby's gtk2 library.
> require "gtk2"
> 
> # Initialize Gtk.
> Gtk.init
> 
> # Create the toplevel window,
> win = Gtk::Window.new
> 
> # Add a title to the program.
> win.set_title "Hello World"
> 
> # Add a 10-pixel border to the edge of the toplevel window canvas.
> win.border_width = 10
> 
> # Create a label to display our "Hello world" message.
> label = Gtk::Label.new "Hello world"
> 
> # Add our newly created label to the toplevel window.
> win.add label
> 
> # Widgets are not automatically visible, so make them visible now.
> # The toplevel window should be made visible last to avoid seeing 
> # widgets and the toplevel window change size as the packing manager
> # change size as the packing manager figures out the appropriate sizes
> # for everything.
> 
> label.show
> win.show
> 
> # Start the event loop.
> Gtk.main
> 
> _______________________________________________
> clue-tech mailing list
> clue-tech at cluedenver.org
> http://www.cluedenver.org/mailman/listinfo/clue-tech


Dennis,

This is not a "flaw". The same problem exists with several programming
languages and platforms.

With GUI programming, it is up to the programmer to hook up the desired
action to every event that is important in a GUI. Clicking the close
button should invoke the necessary steps to close the window
component of the GUI and possibly the event loop, if that is the
desired action.

It should never be assumed that closing a window should automatically
stop the main event loop. After all, it is possible that there are
several open windows in an application. Closing any one of them does
not automatically assume that the application should terminate.

In GUI programming, it is usual to provide some command menu function
(eg, quit) that closes all open windows and terminates the process.
Perhaps, the function also checks whether the work has been saved and
prompts the user accordingly. In this case the window should not be
closed. If the application has a main window, the close button would
typically execute the same function as the command menu quit function.

Hope that helps.

Match



More information about the clue-tech mailing list