O'Reilly logo

Sams Teach Yourself Ruby in 21 Days by Mark Slagell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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
02:
03:    require 'tk'
04:
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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required