Creating a widget

The basic method for creating a widget is the same regardless of the widget being created. The differences are in the properties of the widget and if any action can be assigned to it. In the following chapters, we will investigate specific widgets and their properties in depth, but before we reach this point, I would like to introduce you to the button widget and some basic features.

How to do it…

In the following example, we will create a button widget with some basic properties and a single action. Enter the following commands:


1 % button .b -text "Exit" -command exit
2 % pack .b

At this point, your window should look like the following:

How it works…

The first line instructs the wish shell to create a button named b, which is ...

Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.