Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Part IV.  Tk Widgets

Chapter 27. Buttons and Menus

Buttons and menus are the primary way that applications expose functions to users. This chapter describes how to create and manipulate buttons and menus.

A button widget is associated with a Tcl command that invokes an action in the application. The checkbutton and radiobutton widgets affect an application indirectly by controlling a Tcl variable. A menu elaborates on this concept by organizing button-like items into related sets, including cascaded menus. The menubutton widget is a special kind of button that displays a menu when you click on it.

Tk 8.0 provides a cross-platform menu bar facility. The menu bar is really just a menu that is displayed horizontally along the top of your application's main window. On the Macintosh, the menu bar appears at the top of the screen. You define the menu bar the same on all platforms. Tk 8.0 also uses native button and menu widgets on the Windows and Macintosh platforms. This contributes to a native look and feel for your application. In earlier versions, Tk displayed the widgets identically on all platforms.

Associating a command to a button is usually quite simple, as illustrated by the Tk "Hello, World!" example:

button .hello -command {puts stdout "Hello, World!"}

This chapter describes a few useful techniques for setting up the commands in more general cases. If you use variables inside button commands, you have to understand the scoping rules that apply. This is the first topic of the chapter. Once you get scoping figured out, then the other aspects of buttons and menus are quite straightforward.

      Previous section   Next section