Porting to Tk 4.0

This chapter has notes about upgrading your application from earlier versions of Tk such as Tk 3.6. This includes notable new features that you may want to take advantage of as well as things that need to be fixed because of incompatible changes. The other chapters of this book do not describe some last minute changes to Tk 4.0, which are summarized here.

Porting your scripts from any of the Tk version 3 releases is easy. Not that many things have changed. The sections in this chapter summarize what has changed in Tk 4.0 and what some of the new commands are.

wish

The wish shell no longer requires a -file (or -f) argument, so you can drop this from your script header lines. This flag is still valid, but no longer necessary.

The class name of the application is set from the name of the script file instead of always being Tk. If the script is /usr/local/bin/foobar, then the class is set to Foobar, for example.

Obsolete Features

Several features that were replaced in previous versions are now completely unsupported.

The variable that contains the version number is tk_version. The ancient (version 1) tkVersion is no longer supported.

Button widgets no longer have activate and deactivate operations. Instead, configure their state attribute.

Menus no longer have enable and disable operations. Instead, configure their state attribute.

The cget Operation

All widgets support a cget operation that returns the current value of the specified configuration option. The following two commands are equivalent:

lindex [$w config option] 4

$w cget option

Nothing breaks with this change, but you should enjoy this feature.

Input Focus Highlight

Each widget can have an input focus highlight, which is a border that is drawn in color when the widget has the input focus. This border is outside the border used to draw the 3D relief for widgets. It has the pleasant visual effect of providing a little bit of space around widgets, even when they do not have the input focus. The addition of the input focus highlight does not break anything, but it changes the appearance of your interfaces a little. In particular, the highlight on a canvas obscures objects that are at its edge. See Chapter 23 for a description of the generic widget attributes related to the input focus highlight.

Bindings

The hierarchy of bindings has been fixed so that it is actually useful to define bindings at each of the global (i.e., all), class, and instance levels. The new bindtags command defines the order among these sources of binding information. You can also introduce new binding classes, (e.g. InsertMode), and bind things to that class. Use the bindtags command to insert this class into the binding hierarchy. The order of binding classes in the bindtags command determines the order in which bindings are triggered. Use break in a binding command to stop the progression, or use continue to go on to the next level.

bindtags $w [list all Text InsertMode $w]

The various Request events have gone away: CirculateRequest, ConfigureRequest, MapRequest, and ResizeRequest.

Extra modifier keys are ignored when matching events. While you can still use the Any wild card modifier, it is no longer necessary. The Alt and Meta modifiers are set up in a general way so they are associated with the Alt_L, Alt_R, Meta_L, and Meta_R keysyms.

Scrollbar Interface

The interface between scrollbars and the scrollable widgets has changed. Happily, the change is transparent to most scripts. If you hook your scrollbars to widgets in the straight-forward way, the new interface is compatible. If you use the xview and yview widget commands directly, however, you might need to modify your code. The old interface still works, but there are new features of these operations that give you even better control. You can also query the view state so you do not need to watch the scroll set commands to keep track of what is going on. Finally, scrollable widgets are constrained so that the end of their data remains stuck at the bottom (right) of their display. In most cases, nothing is broken by this change.

Pack info

Version 3 of Tk introduced a new syntax for the pack command, but the old syntax was still supported. This continues to be true in nearly all cases except the pack info command. If you are still using the old packer format, you should probably take this opportunity to convert to the new packer syntax.

The problem with pack info is that its semantics changed. The new operation used to be known as pack newinfo. In the old packer, pack info returned a list of all the slaves of a window and their packing configuration. Now pack info returns the packing configuration for a particular slave. You must first use the pack slaves command to get the list of all the slaves, and then use the (new) pack info to get their configuration information.

Focus

The focus mechanism has been cleaned up to support different focus windows on different screens. The focus command now takes a -displayof argument because of this. Tk now remembers which widget inside each toplevel has the focus. When the focus is given to a toplevel by the window manager, Tk automatically assigns focus to the right widget. The -lastfor argument queries which widget in a toplevel will get the focus by this means.

The focus default and focus none commands are no longer supported. There is no real need for focus default anymore, and focus none can be achieved by passing an empty string to the regular focus command.

The tk_focusFollowsMouse procedure changes from the default explicit focus model where a widget must claim the focus to one in which moving the mouse into a widget automatically gives it the focus.

The tk_focusNext and tk_focusPrev procedures implement keyboard traversal of the focus among widgets. Most widgets have bindings for and that cycle the focus among widgets.

Send

The send command has been changed so that it does not time out after 5 seconds, but instead waits indefinitely for a response. Specify the -async option if you do not want to wait for a result. You can also specify an alternate display with the -displayof option.

The name of an application can be set and queried with the new tk appname command. Use this instead of winfo name ".".

Because of the changes in the send implementation, it is not possible to use send between Tk 4.0 applications and earlier versions.

Internal Button Padding

Buttons and labels have new defaults for the amount of padding around their text. There is more padding now, so your buttons get bigger if you use the default padX and padY attributes. The old defaults were one pixel for both attributes. The new defaults are 3m for padX and 1m for padY, which map into three pixels and ten pixels on my display.

There is a difference between buttons and the other button-like widgets. An extra 2 pixels of padding is added, in spite of all padX and padY settings in the case of simple buttons. If you want your checkbuttons, radiobuttons, menubuttons, and buttons all the same dimensions, you'll need two extra pixels of padding for everything but simple buttons.

Radio Button Value

The default value for a radio button is no longer the name of the widget. Instead, it is an empty string. Make sure you specify a -value option when setting up your radio buttons.

Entry Widget

The scrollCommand attribute changed to xScrollCommand to be consistent with other widgets that scroll horizontally. The view operation changed to the xview operation for the same reason.

The delete operation has changed the meaning of the second index so that the second index refers to the character just after the affected text. The selection operations have changed in a similar fashion. The sel.last index refers to the character just after the end of the selection, so deleting from sel.first to sel.last still works. The default bindings have been updated, of course, but if you have custom bindings you must fix them.

Menus

The menu associated with a menubutton must be a child widget of the menubutton. Similarly, the menu for a cascade menu entry must be a child of the menu.

The @y index for a menu always returns a valid index, even if the mouse cursor is outside any entry. In this case, it simply returns the index of the closest entry, instead of none.

The selector attribute is now selectColor.

The postcascade operation posts the menu of a cascade entry:

$menu postcascade index

The insert operation adds a menu entry before a specified entry:

$menu insert index type options...

Listboxes

Listboxes have changed quite a bit in Tk 4.0. See Chapter 17 for all the details. There are now four Motif-like selection styles, and two of these support disjoint selections. The tk_listboxSingleSelect procedure no longer exists. Instead, configure the selectMode attribute of the listbox. A listbox has an active element, which is drawn with an underline. It is referenced with the active index keyword.

The selection commands for listboxes have changed. Change:

$listbox select from index1
$listbox select to index2

To:

$listbox select anchor index1
$listbox select set anchor index2

The set operation takes two indices, and anchor is a valid index, which typically corresponds to the start of a selection.

You can selectively clear the selection, and query if there is a selection in the listbox. The command to clear the selection has changed. It requires one or two indices. Change:

$listbox select clear

To:

$listbox select clear 0 end

No geometry Attribute

The frame, toplevel, and listbox widgets no longer have a geometry attribute. Use the width and height attributes instead. The geometry attribute got confused with geometry specifications for toplevel windows. The use of width and height is more consistent. Note that for listboxes the width and height is in terms of lines and characters, while for frames and toplevels it is in screen units.

Text Widget

The tags and marks of the text widgets have been cleaned up a bit, justification and spacing is supported, and you can embed widgets in the text display.

A mark now has a gravity, either left or right, that determines what happens when characters are inserted at the mark. With right gravity you get the old behavior: the mark gets pushed along by the inserted text by sticking to the right-hand character. With left gravity it remains stuck. The default is right gravity. The mark gravity operation changes it.

When text is inserted, it only picks up tags that are present on both sides of the insert point. Previously it would inherit the tags from the character to the left of the insert mark. You can also override this default behavior by supplying tags to the insert operation.

The widget scan operation supports horizontal scrolling. Instead of using marks like @y, you need a mark like @x,y.

For a description of the new features, see Chapter 19.

Color Attributes

Table 33-1 lists the names of the color attributes that changed.

Table  33-1  Changes in color attribute names.
Tk 3.6                          Tk4.0
selector                        selectColor
Scrollbar.activeForeground      Scrollbar.activeBackground
Scrollbar.background            troughColor
Scrollbar.foreground            Scrollbar.background
Scale.activeForeground          Scale.activeBackground
Scale.background                troughColor
Scale.sliderForeground          Scale.background
(did not exist)                 highlightBackground
(did not exist)                 highlightColor

Canvas scrollincrement

The canvas widget changed the scrollIncrement attribute to a pair of attributes: xScrollIncrement and yScrollIncrement. The default for these is now one tenth the width (height) of the canvas instead of one pixel. Scrolling by one page scrolls by nine tenths of the canvas display.

The Selection

The selection support has been generalized in Tk 4.0 to allow use of other selections such as the CLIPBOARD and SECONDARY selections. The changes do not break anything, but you should check out the new clipboard command. Some other toolkits, notably OpenLook, can only paste data from the clipboard.

The bell Command

The bell command rings the bell associated with the X display. You need to use the xset program to modify the parameters of the bell such as volume and duration.