Chapter 8. Tk GUI Programming with Tcl/Tk

In this chapter, we will cover:

  • Creating a widget
  • Writing to the console
  • Window manager
  • Creating a window
  • Destroying a window
  • Creating a custom dialog

Introduction

Up to this point, we have exclusively used the Tcl shell and its command set. While this is a wonderful method for writing scripts and non-graphical programs, it fails to provide the end user with a Graphical User Interface (GUI) to interact with. To allow for the creation and control of GUI and window elements, Tcl includes the Graphical Tool Kit (Tk).

Tk is a platform-independent GUI framework included in the Tcl package. By using the native Tk widgets and applying the correct theme, the GUI elements can be made to assume the native look and feel of the system on which they run, providing a polished appearance. Tk provides a full complement of window controls such as buttons, text boxes, and labels. In addition to the standard controls, Tk also includes several top-level dialog windows (for example File Selection, Color Picker, and so on) and includes three methods of geometry management. Additional third-party packages are available to provide expanded functionality and additional controls, many of which are open source. Add to this the fact that almost all features of a widget are customizable during design or runtime, and you are allowed to have a GUI framework that is capable of creating dynamic applications that provide real-time user interaction and feedback with minimal effort on the part of the developer.

As we are now venturing into the creation and manipulation of the GUI, it is more important than ever to ensure the maintainability of your programs. The best method of doing this is by writing your programs with a debugging mindset. What I mean by this is to keep in mind the future requirements to read and understand your, when you design and write your programs.

Imagine the number of commands, windows and widgets (A widget is a Tk control; text box, label, image, and so on) that are required for a standard data entry interface. Buttons, text boxes, menus, labels, and all the other items necessary for interacting with the end user each have a potentially large amount of coding involved. If you design and write with the requirement of returning to the program, or the requirement for another programmer to perform these actions, you can greatly simplify the maintenance of your programs.

I have found that I can address most, if not all of these concerns with a few simple actions. First consider the syntax. Tk allows you the freedom to display widgets in a very free form manner. However, by following a consistent method of widget creation you can speed up the maintenance and greatly improve the readability of your coding. While there are no set rules, try to follow a standardized methodology in how your widgets are defined.

As you include error handling in your programs keep the concept of maintainability in mind at all times. Non-descriptive or worse, humorous, error messages and logs can not only result in an unprofessional product, but create a situation where the upkeep of your program is frustrating at best and downright embarrassing in some situations. I have seen an "Oops, something went wrong!" error message on more occasions than I care to remember.

To access and control our widgets, we will need to activate the Window Shell (wish) in the same manner as the Tcl Shell (tclsh). To access the wish shell, simply enter wish at the command line. As soon as you enter the wish command, Tk will create a window on which all the widgets will be displayed as in the following image:

Introduction

Any widget based on window commands will automatically update the window as soon as you press Enter, unless of course there is an error. In the event of an error, it will be displayed within the wish console. In the following sections, we will introduce widgets, writing to the wish console, and basic windowing.

To complete the following examples, we will need to access wish from the command line. Launch the wish shell appropriately for your operating system and follow the instructions provided in each section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.63