Our First Tk Application

The Ruby/Tk binding code lives in a file named tk.rb somewhere in the load path. We need to require this file in any Ruby script that will use the Tk toolkit.

Listing 17.1 is our first Tk script.

Listing 17.1. A "Hello, World" Application in Ruby/Tk
01:    #!/usr/bin/env ruby
03:    require 'tk'
05:    root = TkRoot.new { title "Our First Tk App" }
06:    label = TkLabel.new(root) do
07:      text "Hello, world!"
08:      pack("padx" => 90)
09:    end
10:    Tk.mainloop

Figure 17.1 shows how this program appears on the screen when it's run. (Note that this screenshot and all of the others in this chapter were produced on a Microsoft Windows platform. On other platforms the results may look different, but not significantly so.)

Figure ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.