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.
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.
foreach app [winfo interps] { catch {send $app [list Iam [tk appname]]} } |
Table 41-5 summarizes these commands:
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. |
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:
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. |
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.
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:
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. |
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.
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.
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:
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.
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:
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:
3.141.28.107