macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Ruby/Tk Primer, Part 3

by Christopher Roach
07/27/2004

Editor's note: In this third and final installment of this multi-part series on Ruby/Tk programming, Christopher Roach pulls together all the pieces and shows you how to complete your demo application.

A Sample Ruby/Tk Application

After our explorations in Part 2 of this series, we now have a good understanding of what the Tk library can do. But we have yet to put it to use. So, before jumping into our cron application, let's take a look at a simple Ruby/Tk application.

The code below displays a simple window containing a single button that asks the user to "Click Me." When you click on the button, you should see a nice congratulatory message for creating your very first Ruby/Tk program printed to the console. Go ahead and copy the code into your favorite editor (make sure to use Unix line endings), and try running the program.


# Load the Tk module
require 'tk'

# Create the root frame for the app
root = TkRoot.new('title'=>'My First Ruby/Tk Application')

# Create a new button in the center of the frame with
#'Click Me' as its text
TkButton.new(root) {
    text    'Click Me'
    command proc {
	    puts "Congratulations on creating" +
            "your first Ruby/Tk" +
		      "application"}
}.pack('padx'=>15, 'pady'=>15)

# Start the Tk main event loop
Tk.mainloop()

Let's take a closer look at the code of our very first Ruby/Tk application. The first thing you'll notice is that we've required the Tk extension module. This module is just a collection of the classes and methods associated with the Tk library. Next, you'll notice that we created an instance of the TkRoot class.

As I stated in the list of widgets in Part 2, the TkRoot class is a special type of TkFrame. It's mainly a container for other widgets, and all of our Ruby/Tk applications should have an instance of the TkRoot class as the widget highest up in the widget hierarchy.

After we've created our main widget container object, we go on to create a TkButton object. When we create an instance of a Tk widget we are expected to tell its new method the widget in which the new widget will reside. In this case, the button we create will reside within the root frame object. You'll notice that we have also supplied a code block to the new method of the TkButton class.

In this code block we set the options for our widget. or the new button we set the text to "Click Me," and we passed in an action for the button to take when it is pressed by creating a Proc object with another code block that calls the puts method. Thus, when we click the button, we should see a congratulatory message printed to the console.

There are a couple of very important things to notice here before creating our cron manager GUI. First, any code blocks used in a Ruby program must begin on the same line as the method to which they belong. You'll notice that the code block of options for the TkButton class begins on the same line as the new method. Move the opening brace ({) to the next line and notice that you'll receive an error when it is executed.

Next, take note of the pack command after the TkButton's options code block. If you remove this command from the application and run it again, you should get a window with no button. If you remember in the last section, I explained that in order to see a widget it must first be registered with a geometry manager. Well, that is exactly what we are doing here with the call to the pack command. Essentially, we are notifying the root frame's packer geometry manager of our button widget and we are telling it to pack the root frame around it with a space of 15 pixels between the button and the edges of the root frame.

Last, but not least, you'll notice a call to the mainloop method in the Tk module (actually, if you're a stickler for exactness, the mainloop method can be found within the TkCore module, it can be accessed in the Tk module through an include of TkCore). The mainloop method starts the Tk GUI event loop. This is an infinite loop that basically listens for event notifications from the underlying operating system and executes the callback methods associated with each event.

Now that we've created our first Ruby/Tk application, and we understand the general makeup of a Ruby/Tk-based program, we can move on to coding the GUI for our cron manager application. The next section will walk you through the code that creates our application's GUI and binds it to the backend we created in the first article.

Designing the Cron Manager GUI

Before we get started programming our cron GUI front-end, I thought we might want to have a look at the finished product just so you know exactly what we are going to create. The picture below is how the final product will look. Take note of the widgets used in the interface so you know exactly what needs to be created in your initialize method.

Cron Manager

From the image above, you may notice that we will have to create a listbox, a scrollbar, five drop-down lists (option menu buttons in Tk), a text field (entry field in Tk), six labels, and five buttons. We will also need two frames for the layout of the GUI. You should have also noticed a few of the methods we'll need in our cron manager program by looking at the names of the buttons.

We will obviously need callback methods for adding a new cron job, updating a cron job, deleting a cron job, and saving the list of cron jobs as well as a callback for the listbox that will set the other widgets to the information associated with the currently selected item. Incidentally, the little button with the ellipses (...) will be used for browsing for the command we wish to schedule with our cron manager. So, we will also need another callback method for the browse button.

As a quick side note, when you are creating an application like this, it's useful to layout the interface with another application first. This gives your a nice mock-up before you start programming. I used Interface Builder to create my interface layout before I started. It really helped out since I was able to get the look and feel just right before
I began programming. Once I started programming the GUI it was like programming with a roadmap since I had an interface mock-up to look at whenever I got stuck.

Creating a mock-up of my interface also allowed me to get a list of all the widgets and callback methods I needed before I got started and I was able to determine how I was going to use frames and geometry managers to layout my design without writing a single piece of code. And I was able to determine how I was going to use frames and geometry managers to layout my design without writing a single piece of code. In the end, the only things I had to think about in my code were what extra helper methods had to be included to get my interface working properly.

Now that we have our design created for our cron manager we need to move on to the coding portion of our tutorial. There's quite a bit of code in our CronManager.rb file so your going to need to get a copy of the file here.

With a copy of the file in hand, we can go ahead and start deciphering it to find out what makes it tick.

Pages: 1, 2

Next Pagearrow