[ Team LiB ] Previous Section Next Section

The winfo Command

The winfo command has about 50 operations that return information about a widget or the display. The operations fall into the following categories:

  • Sending commands between applications.

  • Family relationships.

  • Size.

  • Location.

  • Virtual root coordinates.

  • Atoms and IDs.

  • Colormaps and visuals.

Sending Commands between Applications

Each Tk application has a name that is used when sending commands between applications using the send command, which is described in Chapter 43. The list of Tk applications is returned by the interps operation. The tk appname command is used to get the name of the application, and that command can also be used to set the application name.

Example 44-2 shows how your application might connect up with several existing applications. It contacts each registered Tk interpreter and sends a short command that contains the application's own name as a parameter. The other application can use that name to communicate back.

Example 44-2 Telling other applications what your name is
foreach app [winfo interps] {
   catch {send $app [list Iam [tk appname]]}
}

Table 44-5 summarizes these commands:

Table 44-5. send command information

tk appname ?newname?

Queries or sets the name used with send.

winfo name .

Also returns the name used for send, for backward compatibility with Tk 3.6 and earlier.

winfo name pathname

Returns the last component of pathname.

winfo ?-displayof win? interps

Returns the list of registered Tk applications on the same display as win.

Widget Family Relationships

The Tk widgets are arranged in a hierarchy, and you can use the winfo command to find out about the structure of the hierarchy. The winfo children operation returns the children of a window, and the winfo parent operation returns the parent. The parent of the main window is null (i.e., an empty string).

A widget is also a member of a class, which is used for bindings and as a key into the resource database. The winfo class operation returns this information. You can test for the existence of a window with winfo exists, and whether or not a window is mapped onto the screen with winfo viewable. Note that winfo ismapped is true for a widget that is managed by a geometry manager, but if the widget's top-level window is not mapped, then the widget is not viewable.

The winfo manager operation tells you what geometry manager is controlling the placement of the window. This returns the name of the geometry manager command. Examples include pack, place, grid, canvas, and text. The last two indicate the widget is embedded into a canvas or text widget.

Table 44-6 summarizes these winfo operations:

Table 44-6. Window hierarchy information

winfo children win

Returns the list of children widgets of win.

winfo class win

Returns the resource class of win.

winfo exists win

Returns 1 if win exists.

winfo ismapped win

Returns 1 if win is mapped onto the screen.

winfo manager win

Geometry manager: pack, place, grid, canvas, or text.

winfo parent win

Returns the parent widget of win.

winfo viewable win

Returns 1 if win and all its parent windows are mapped.

Widget Size

The winfo width and winfo height operations return the width and height of a window, respectively. Alternatively, you can ask for the requested width and height of a window. Use winfo reqwidth and winfo reqheight for this information. The requested size may not be accurate, however, because the geometry manager may allocate more or less space, and the user may resize the window.

graphics/common_icon.gif

Size is not valid until a window is mapped.


A window's size is not set until a geometry manager maps a window onto the display. Initially, a window starts out with a width and height of 1. You can use tkwait visibility to wait for a window to be mapped before asking its width or height, or you can use update to give Tk a chance to update the display. There are some potential problems with update that are discussed on page 608. Dialog_Wait in Example 39-1 on page 606 uses tkwait visibility.

The winfo geometry operation returns the size and position of the window in the standard geometry format: WxH+X+Y. In this case the X and Y offsets are relative to the parent widget, or relative to the root window in the case of the main window.

You can find out how big the display is, too. The winfo screenwidth and winfo screenheight operations return this information in pixels. The winfo screenmmwidth and winfo screenmmheight return this information in millimeters.

You can convert between pixels and screen distances with the winfo pixels and winfo fpixels operations. Given a number of screen units such as 10m, 3c, or 72p, these return the corresponding number of pixels. The first form rounds to a whole number, while the second form returns a floating point number. The correspondence between pixels and sizes may not be accurate because users can adjust the pixel size on their monitors, and Tk has no way of knowing about that. Chapter 40 explains screen units on page 612. For example:

set pixelsToInch [winfo pixels . 2.54c]

Table 44-7 summarizes these operations:

Table 44-7. Window size information

winfo fpixels win num

Converts num, in screen units, to pixels. Returns a floating point number.

winfo geometry win

Returns the geometry of win, in pixels and relative to the parent in the form WxH+X+Y

winfo height win

Returns the height of win, in pixels.

winfo pixels win num

Converts num to a whole number of pixels.

winfo reqheight win

Returns the requested height of win, in pixels.

winfo reqwidth win

Returns the requested width of win, in pixels.

winfo screenheight win

Returns the height of the screen, in pixels.

winfo screenmmheight win

Returns the height of the screen, in millimeters.

winfo screenmmwidth win

Returns the width of the screen, in millimeters.

winfo screenwidth win

Returns the width of the screen, in pixels.

winfo width win

Returns the width of win, in pixels.

Widget Location

Table 44-8. Window location information

winfo containing ?-displayof win? win x y

Returns the pathname of the window at x and y.

winfo pointerx win

Returns the X screen coordinate of the mouse.

winfo pointery win

Returns the Y screen coordinate of the mouse.

winfo pointerxy win

Returns the X and Y coordinates of the mouse.

winfo rootx win

Returns the X screen position of win.

winfo rooty win

Returns the Y screen position of win.

winfo screen win

Returns the display identifier of win's screen.

winfo server win

Returns the version string of the display server.

winfo toplevel win

Returns pathname of toplevel that contains win.

winfo x win

Returns the X position of win in its parent.

winfo y win

Returns the Y position of win in its parent.

The winfo x and winfo y operations return the position of the upper-left corner of a window relative to its parent widget. In the case of the main window, this is its location on the screen. The winfo rootx and winfo rooty return the screen location of the upper-left corner of a widget, even if it is not a toplevel.

The winfo containing operation returns the pathname of the window that contains a point on the screen. This is useful in implementing menus and drag-and-drop applications.

The winfo toplevel operation returns the pathname of the toplevel that contains a widget. If the window is itself a toplevel, then this operation returns its own pathname.

The winfo screen operation returns the display identifier for the screen of the window.

Virtual Root Window

Some window managers use a virtual root window to give the user a larger virtual screen. At any given time, only a portion of the virtual screen is visible, and the user can change the view on the virtual screen to bring different applications into view. In this case, the winfo x and winfo y operations return the coordinates of a main window in the virtual root window (i.e., not the screen).

The winfo vrootheight and winfo vrootwidth operations return the size of the virtual root window. If there is no virtual root window, then these just return the size of the screen.

graphics/common_icon.gif

Correcting virtual root window coordinates.


The winfo vrootx and winfo vrooty are used to map from the coordinates in the virtual root window to screen-relative coordinates. These operations return 0 if there is no virtual root window. Otherwise, they return a negative number. If you add this number to the value returned by winfo x or winfo y, it gives the screen-relative coordinate of the window:

set screenx [expr [winfo x $win] + [winfo vrootx $win]]

Table 44-9 summarizes these operations:

Table 44-9. Virtual root window information

winfo vrootheight win

Returns the height of the virtual root window for win.

winfo vrootwidth win

Returns the width of the virtual root window for win.

winfo vrootx win

Returns the X position of win in the virtual root.

winfo vrooty win

Returns the Y position of win in the virtual root.

Atoms and IDs

An atom is an X technical term for an identifier that is registered with the X server. Applications map names into atoms, and the X server assigns each atom a 32-bit identifier that can be passed between applications. One of the few places this is used in Tk is when the selection mechanism is used to interface with different toolkits. In some cases the selection is returned as atoms, which appear as 32-bit integers. The winfo atomname operation converts that number into an atom (i.e., a string), and the winfo atom registers a string with the X server and returns the 32-bit identifier as a hexadecimal string

Each widget has an ID assigned by the window system. The winfo id command returns this identifier. The winfo pathname operation returns the Tk pathname of the widget that has a given ID, but only if the window is part of the same application.

graphics/common_icon.gif

Embedding applications.


The id operation is useful if you need to embed another application into your window hierarchy. Wish takes a -use id command-line argument that causes it to use an existing window for its main window. Other toolkits provide similar functionality. For example, to embed another Tk app in a frame:

frame .embed -container true
exec wish -use [winfo id .embed] otherscript.tcl

Table 44-10 summarizes these operations:

Table 44-10. Atom and window ID information

winfo atom ?-displayof win? name

Returns the 32-bit identifier for the atom name.

winfo atomname ?-displayof win? id

Returns the atom that corresponds to the 32-bit ID.

winfo id win

Returns the window ID of win.

winfo pathname ?-displayof win? id

Returns the Tk pathname of the window with id, or null.

Colormaps and Visuals

The winfo depth returns the number of bits used to represent the color in each pixel. The winfo cells command returns the number of colormap entries used by the visual class of a window. These two values are generally related. A window with 8 bits per pixel usually has 256 colormap cells. The winfo screendepth and winfo screencells return this information for the default visual class.

The winfo visualsavailable command returns a list of the visual classes and screen depths that are available. For example, a display with 8 bits per pixel might report the following visual classes are available:

winfo visualsavailable .
=> {staticgray 8} {grayscale 8} {staticcolor 8} \
     {pseudocolor 8}

The winfo visual operation returns the visual class of a window, and the winfo screenvisual returns the default visual class of the screen.

The winfo rgb operation converts from a color name or value to the red, green, and blue components of that color. Three decimal values are returned. Example 41-2 on page 624 uses this command to compute a slightly darker version of the same color.

Table 44-11 summarizes operations that return information about colormaps and visual classes, which are described in Chapter 41:

Table 44-11. Colormap and visual class information

winfo cells win

Returns the number of colormap cells in win's visual.

winfo colormapfull win

Returns 1 if the last color allocation failed.

winfo depth win

Returns the number of bits per pixel for win.

winfo rgb win color

Returns the red, green, and blue values for color.

winfo screencells win

Returns the number of colormap cells in the default visual.

winfo screendepth win

Returns the number of bits per pixel in the screen's default visual.

winfo screenvisual win

Returns the default visual of the screen.

winfo visual win

Returns the visual class of win.

winfo visualsavailable win

Returns a list of pairs that specify the visual type and bits per pixel of the available visual classes.

    [ Team LiB ] Previous Section Next Section