1. Home
  2. Computing & Technology
  3. Ruby

Tk Widgets

By , About.com Guide

2 of 6

Creating a Window

Empty Tk Window

Before you can add any widgets, you must have something to add them to. In Tk, this is called the root window. All widgets in the application are inside of and belong to this one root window. The root window also defines some other things, such as the size and title of the window.

To create a new root window, create a new TkRoot object. You'll notice that the new method takes a block as an argument. This is the way arguments are passed to Tk widgets when they're created. Within the block, there are a number of methods available to further describe how the widget looks or acts. In the following example, a new TkRoot object is created and, within the block, the title method is used to give the window a title. Once the TkRoot object is created, the Tk.mainloop method is run. This will run the Tk program and return to the Ruby program when the Tk window is closed.

#!/usr/bin/env ruby
require 'tk'

root = TkRoot.new do
title "Tk / Ruby"
end

Tk.mainloop
Explore Ruby
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. GUI
  5. Tk
  6. Creating a Window

©2010 About.com, a part of The New York Times Company.

All rights reserved.