| [ Team LiB ] |
|
The winfo CommandThe 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 ApplicationsEach 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:
Widget Family RelationshipsThe 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:
Widget SizeThe 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. 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:
Widget Location
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 WindowSome 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. 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:
Atoms and IDsAn 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. 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:
Colormaps and VisualsThe 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:
|
| [ Team LiB ] |
|