X Window System Programming with Tcl and Tk
One short note about Tcl syntax: Braces ( { ... }) are used to group a set of Tcl commands together as a ”sub-script“. The commands contained within the braces are passed to the Td interpreter without performing variable substitution. This is an important concept to understand. Without the braces, the Tcl interpreter would have attempted to substitute the value of the variable fname while interpreting the bind command in the script. That is, when the bind command is first executed, the variable fname has no value. Without braces, the Tcl interpreter would complain that fname is an unknown variable. Using braces, however, we delay the interpretation of $fname until the event binding is actually executed; in this case, when Return is pressed in the entry widget.
Tk is a set of extensions to Tcl which implement commands... permitting you to write X applications as simple Tcl scripts.
Note that Tcl has several odd rules with respect to line breaks. Tcl expects each command to consist of a single line; the end of a line indicates the end of a command, unless the line ends with a backslash, the same as in shell scripts. However, if a line ends in an opening brace, Tcl understands that you are beginning a sub-script, to be contained within braces, and continues to read the script until a closing brace. For this reason, you can't say:
bind .e <Return>
{
exec xterm -e vi $fname
{
Tcl will think that the bind command ends after the first line, and complain that it needs a script to execute for the event binding. Therefore, when using braces to encapsulate a sub-script, be sure that the opening brace is at the end of the line beginning the script.
In Tk, widgets are named in a hierarchial fashion. The topmost ”shellH widget (that is, the main wish window) is named “ . ” (dot). All widgets which are direct childIen of . are given names beginning with ., such as .b,.entry,.leftscroll, and so forth. The widget name can be any alphanumeric string beginning with a dot; you choose the widget name when you create the widget, using commands such as button and label. Further subwidgets are given names such as .foo.bar.bar, where each level is separated with a dot.
For example, you might have a menu bar widget named .mbar. It is a child, of course, of ., the main window. Menu buttons contained within the menu bar might be named .mbar.file,.mbar.options, and so forth. That is, the menu bar is a child of the main application window, and the individual menu buttons are children of the menu bar. Arranging widgets into a hierarchy allows you to group them together for logical and visual purposes. I'll cover this in more detail later.
In order to demonstrate the power of Tcl/Tk, I'm going to present an actual application, written entirely as a Tcl/Tk script. As I go along I will describe the syntax used and the features available. You can use the Tcl/Tk man pages to fill in the gaps.
This program is a simple drawing application, utilizing the Tk canvas widget. The canvas is a simple graphics display widget which will display various kinds of objects: rectangles, lines, text, ovals, and so forth. What we're going to do is combine the canvas widget with the user interface capabilities of Tk to allow the user to draw objects using the mouse.
Figure 3 demonstrates what our application looks like when used. The Color menu has been pulled down so that you can see the various selections available.
The entire script, draw.tcl, is given here. Note that it's less than 200 lines long. This is amazingly short for such a complicated X application involving menus, colors, mouse input, and so forth. If you don't feel like entering this entire script, the code is available via ftp from sunsite.unc.edu, in the directory /pub/Linux/docs/LJ.
At first glance, this script might appear to be complicated. There are a few rough spots, but the overall concepts presented here are quite simple. Let's take a closer look at this program, but let's start near the middle of the script, where we create the frame widget:
frame .mbar -relief groove -bd pack .mbar -side top -expand yes -fill x
The frame command creates a frame widget, which is used to group widgets together. The frame itself is usually invisible, unless you specify that a border should be drawn around it.
Here we create a frame named .mbar, and specify that it should use the groove relief type. The “relief” of a widget indicates what kind of 3D border should appear around the widget. (Many options, such as -relief, bd, -foreground, and -background are supported by all widget types.) The valid types for -relief are:
raised: Makes widget appear to be raised on display.
sunken: Makes widget appear to sink into display.
ridge: Draw raised ridge around widget border.
groove: Draw sunken groove around widget border.
flat: No relief; appear as if flat.
The -ted option specifies the border width to use for the widget (in this case, the width of the groove). Here, we set the border width to 3 pixels.
Next, we pack .mbar into the wish window. (By default, widgets are packed into their direct parent. In this case, the parent of .mbar is ., the topmost window). The -side argument to pack indicates which side of the parent we should pack .mbar into. The -expand yes option indicates that the widget should be given all of the extra space around it. Because we are packing the widget into the top edge of the window, the -expand option gives the widget any extra horizontal space to its left and right. The -fill x command causes the widget to grow until it fills this space. Using -expand yes without -fill would give the widget the extra horizontal space, but the widget wouldn't grow to fill that space. (If you're interested in how this works, experiment with the pack command in various forms. Also, see the pack man page or Ousterhout's book for more details.)
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- RSS Feeds
- Validate an E-Mail Address with PHP, the Right Way
- Readers' Choice Awards
- Tech Tip: Really Simple HTTP Server with Python
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?





48 min 5 sec ago
1 hour 20 min ago
3 hours 44 min ago
3 hours 47 min ago
3 hours 48 min ago
8 hours 13 min ago
10 hours 4 min ago
15 hours 17 min ago
18 hours 29 min ago
20 hours 44 min ago