1. Computing

Tk Widgets

By

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
See More About
  1. About.com
  2. Computing
  3. Ruby
  4. Graphical User Interfaces
  5. Tk
  6. Adding Text Fields

©2014 About.com. All rights reserved.