1. Computing & Technology

Discuss in my forum

Tk Widgets

By , About.com Guide

See More About:
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

©2012 About.com. All rights reserved.

A part of The New York Times Company.