The winfo Command

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 41.  Window Managers and Window Information


The winfo Command

The winfo command has just over 40 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 40. 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. In Tk 3.6 and earlier, you had to use winfo name . to get the name of the application.

Example 41-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 41-2 Telling other applications what your name is.
 foreach app [winfo interps] {    catch {send $app [list Iam [tk appname]]} } 

Table 41-5 summarizes these commands:

Table 41-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 pathnameReturns the last component of pathname.
winfo ?-displayof win? interpsReturns the list of registered Tk applications on the same display as win.

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 41-6 summarizes these winfo operations:

Table 41-6. Window hierarchy information.
winfo children winReturns the list of children widgets of win.
winfo class winReturns the resource class of win.
winfo exists winReturns 1 if win exists.
winfo ismapped winReturns 1 if win is mapped onto the screen.
winfo manager winGeometry manager: pack, place, grid, canvas, or text.
winfo parent winReturns the parent widget of win.
winfo viewable winReturns 1 if win and all its parent windows are mapped.

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/tip_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 522. Dialog_Wait in Example 36-1 on page 519 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 37 explains screen units on page 526. For example:

 set pixelsToInch [winfo pixels . 2.54c] 

Table 41-7 summarizes these operations:

Table 41-7. Window size information.
winfo fpixels win numConverts num, in screen units, to pixels. Returns a floating point number.
winfo geometry winReturns the geometry of win, in pixels and relative to the parent in the form WxH+X+Y
winfo height winReturns the height of win, in pixels.
winfo pixels win numConverts num to a whole number of pixels.
winfo reqheight winReturns the requested height of win, in pixels.
winfo reqwidth winReturns the requested width of win, in pixels.
winfo screenheight winReturns the height of the screen, in pixels.
winfo screenmmheight winReturns the height of the screen, in millimeters.
winfo screenmmwidth winReturns the width of the screen, in millimeters.
winfo screenwidth winReturns the width of the screen, in pixels.
winfo width winReturns the width of win, in pixels.

Location

Table 41-8. Window location information.
winfo containing ?-displayof win? win x yReturns the pathname of the window at x and y.
winfo pointerx winReturns the X screen coordinate of the mouse.
winfo pointery winReturns the Y screen coordinate of the mouse.
winfo pointerxy winReturns the X and Y coordinates of the mouse.
winfo rootx winReturns the X screen position of win.
winfo rooty winReturns the Y screen position of win.
winfo screen winReturns the display identifier of win's screen.
winfo server winReturns the version string of the display server.
winfo toplevel winReturns pathname of toplevel that contains win.
winfo x winReturns the X position of win in its parent.
winfo y winReturns 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/tip_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 41-9 summarizes these operations:

Table 41-9. Virtual root window information.
winfo vrootheight winReturns the height of the virtual root window for win.
winfo vrootwidth winReturns the width of the virtual root window for win.
winfo vrootx winReturns the X position of win in the virtual root.
winfo vrooty winReturns 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/tip_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 41-10 summarizes these operations:

Table 41-10. Atom and window ID information.
winfo atom ?-displayof win? nameReturns the 32-bit identifier for the atom name.
winfo atomname ?-displayof win? idReturns the atom that corresponds to the 32-bit ID.
winfo id winReturns the window ID of win.
winfo pathname ?-displayof win? idReturns 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 38-2 on page 537 uses this command to compute a slightly darker version of the same color.

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

Table 41-11. Colormap and visual class information.
winfo cells winReturns the number of colormap cells in win's visual.
winfo colormapfull winReturns 1 if the last color allocation failed.
winfo depth winReturns the number of bits per pixel for win.
winfo rgb win colorReturns the red, green, and blue values for color.
winfo screencells winReturns the number of colormap cells in the default visual.
winfo screendepth winReturns the number of bits per pixel in the screen's default visual.
winfo screenvisual winReturns the default visual of the screen.
winfo visual winReturns the visual class of win.
winfo visualsavailable winReturns a list of pairs that specify the visual type and bits per pixel of the available visual classes.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net