1. Home
  2. Computing & Technology
  3. Ruby

Tk Widgets

By Amanda & Michael Morin, About.com

4 of 6

Adding Text Fields

Tk window with button and two entry widgets

Now that the button has been added to the window, it's time to add the text fields for the two numbers. The text fields are created by creating new TkEntry objects. The same pattern as was used for adding the button is used here. A new object is created as a child widget of the root; its options are defined in the block and then the pack method is called. The following code will add the two TkEntry objects.

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

root = TkRoot.new do
title "Adder"
end

button = TkButton.new(root) do
text "Add"
pack
end

entry1 = TkEntry.new(root) do
pack
end

entry2 = TkEntry.new(root) do
pack
end

Tk.mainloop
Explore Ruby
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. GUI
  5. Tk
  6. Adding Text Fields

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

All rights reserved.