Creating Windows With Ruby Tk

The Ruby language supports creating windows for GUI (Graphic User Interface). GUI applications are programs that respond to user input, which are usually mouse events, such as clicking buttons, clicking menus, etc.

A GUI application may consist of the following types of objects:

  • Widgets – basic GUI objects that can be put directly in the window. Most of them generate events as a response to user actions. A label is a widget, too, but, usually does not generate events.
  • Shapes – lines, arcs, circles, polygons and other that belong on a canvas.
  • Timers – threads that perform an action the number of times specified and sleep for the  specified duration. From the definition “thread” you can understand that they run in parallel.

The simplest Ruby Tk program is:


require 'tk'
Tk.mainloop

This program displays the following window:

This is the default window. It is displayed on the screen when the line ‘Tk.mainloop’ is performed. Until this window is closed, no commands that are not responses to events will be executed.

“Programing Ruby – The Pragmatic Programmers Guide” suggests that you look at Perl/Tk guides to learn how to use Tk. A good place to look for Perl’s objects and their methods is Active Perl. I’m not going to write here the complete guide to Ruby Tk, but I hope the following chapters will help you understand how it works.

To be continued.

Advertisement