X Window System Programming with Tcl and Tk
Having created and packed our menu bar, we create three menu items, using the menubutton command:
menubutton .mbar.file -text "File" -underline 0 \
-menu .mbar.file.menu
menubutton .mbar.obj -text "Object" -underline 0 \
-menu .mbar.obj.menu
menubutton .mbar.color -text "Color" -underline 0 \
-menu .mbar.color.menu
pack .mbar.file .mbar.obj .mbar.color -side left
A menubutton widget is simply a button which, when pressed, causes a menu to appear below it. First, note that the menu buttons are child widgets of the .mbar widget. The -text option specifies the text to be displayed within the menubutton, and the -underline option specifies the index of the letter in the menubutton text to underline for keyboard shortcuts. In each case, we underline the first letter of the string (see: Figure 3).
The -menu option specifies the name of the menu widget which should appear when the button is pressed. We haven't created these widgets (.mbar.file.menu, and so on) yet, but do so immediately thereafter.
First, we create the File men
menu .mbar.file.menu
.mbar.file.menu add command -label \
"Save PostScript..." -command { get_ps
.mbar.file.menu add command -label "Quit"
-command { exit }
The menu command creates a menu widget with the given name. (Note that the menu .mbar.file.menu is a child widget of the menubutton .mbar.file). We then add menu entries to this widget using the add menu widget command.
Note that widget names themselves are commands. In general, if we want to perform a function on a specific widget, we invoke it as a command and use various widget subcommands. For example, to modify the appearance of a widget, we can use the widget subcommand configure:
<widget name> configure [ <options> ... ]
For example,
.mbar configure -background blue
would change the .mbar widget background color to blue. The widget man pages included with Tcl/Tk describe which subcommands are available with each widget type.
In the example above, we use the menu subcommand add to add entries to a menu. The syntax is
<widget name> add <entry type> [ <options> ... ]
where <entry type> is one of:
command-: Like a button widget, invokes a Tcl command when selected. radictutton- A group of radicLutton entries controls the value of a named variable. One radiobutton in the group (that is, only one radiobutton associated with a given variable) may be activated at any given time.
checkbutton: Similar to radicLutton. Will toggle the value of a variable to either 0 (off) or 1 (on). Unlike radiobuttons, how“ ever, checkbuttons are not mutually exclusive with one another.
cascade: Allows you to ”cascade“ sub-menus within the current menu.
separator: A nonfunctional menu separator, used to visually divide menu entries.
The menu man page describes these in more detail. Within the File menu, we create two command entries. When selected, each entry executes the command given by the -command argument. The -label argument, as you can guess, assigns a text label to the menu entry.
The Object menu is similar in nature to the File menu, except that it uses radiobutton entries. These entries are linked to the variable object_type. When we select the Ovals option from the menu, object_type is set to oval. Likewise, when selecting Rectangles, the variable is set to rect. Because these are radiobuttons, only one may be selected at a time. Later, we'll see how the object_type variable affects object drawing within the canvas widget.
The Color menu is like the Object menu: We have four radiobutton entries linked to the variable thecolor. We use the -background option with these entries to visually depict the color being selected.
After creating the menus, we use the tk_menubar command to tell Tk that this is the primary menu bar for our application. This enables keyboard shortcuts for the menu bar. If you press Alt along with one of the underlined letters in a menu title, that menu will be activated. For example, pressing Alt-F will activate the File menu. The -underline argument to the menubutton command controls which letter activates which menu. Pressing F10 activates the leftmost menu, and you can use the arrow keys to move around.
The focus command is used to cause all keyboard events in the application window to be received by the menu bar. Otherwise, the mouse pointer would have to be within the menu bar for the keyboard shortcut events to be received. This is yet another fine point about X programming which you shouldn't concern yourself with at this point.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- New Products
- Trying to Tame the Tablet
- What's the tweeting protocol?
- Validate an E-Mail Address with PHP, the Right Way
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




2 hours 28 min ago
4 hours 51 min ago
21 hours 39 min ago
1 day 11 min ago
1 day 1 hour ago
1 day 2 hours ago
1 day 2 hours ago
1 day 7 hours ago
1 day 8 hours ago
1 day 9 hours ago