Widget(3pm)
okay so welcome to Widget(3pm) β this is THE page, the blueprint, the mother of all perl/Tk widgets π
if you're building a mega-widget or you just wanna know what every single widget in the universe can do, you're in the right place, bestie π«Ά
β¨ NAME β¨
Tk::Widget</p> β Base class of all widgets π
β¨ SYNOPSIS β¨
package Tk::Whatever;
require Tk::Widget;
@ISA = qw(Tk::Widget);
Construct Tk::Widget 'Whatever';
sub Tk_cmd { \&Tk::whatever }
$widget->method(?arg, arg, ...?)
β¨ DESCRIPTION β¨
Tk::Widget is an abstract base class for all Tk widgets. Like, it's the parent of everything. Every widget you've ever loved? Child of this. π
The generic methods available to all widgets include the ones based on the core winfo mechanism, and they're used to retrieve info about windows managed by Tk. They can take any of a bunch of different forms depending on the method. The legal forms are β okay, here's the whole list, no skipping, we're being thorough β¨
π $widget->appname?(newName)?
If newName isn't specified, this method returns the name of the application β that's the name that may be used in send commands to communicate with the application π«Ά
If newName is specified, then the name of the application is changed to newName. If the given name is already in use, then a suffix of the form " #2" or " #3" gets appended to make the name unique. The method's result is the name actually chosen, so you always know what you got.
newName should not start with a capital letter. This will interfere with option processing, since names starting with capitals are assumed to be classes; as a result, Tk may not be able to find some options for the application. π
If sends have been disabled by deleting the send command, this command will re-enable them and recreate the send command. iconic behaviour honestly.
π $widget->atom(name)
Returns a decimal string giving the integer identifier for the atom whose name is name. If no atom exists with the name name then a new one is created. β¨
π $widget->atomname(id)
Returns the textual name for the atom whose integer identifier is id. This command is the inverse of the $widget->atom command. It generates an error if no such atom exists. π
π $widget->bell( ?-nice? );
This command rings the bell on the display for $widget and returns an empty string. It uses the current bell-related settings for the display, which may be modified with programs such as xset.
If -nice is not specified, this command also resets the screen saver for the screen. Some screen savers will ignore this, but others will reset so that the screen becomes visible again. π
π $widget->bindDump
This command returns a list of strings suitable for printing, detailing binding information for a widget. It prints a widget's bindtags. For each binding tag it prints all the bindings, comprised of the event descriptor and the callback. Callback arguments are printed, and Tk::Ev objects are expanded. π¦
π $widget->Busy?(?-recurse => 1?,-option => value?)?
This method configures a -cursor option for $widget and β if -recurse => 1 is specified β all its descendants. The cursor to be set may be passed as -cursor => cursor or defaults to 'watch'. Additional configure options are applied to $widget only. π
It also adds a special tag 'Busy' to the bindtags of the widgets so configured, so that KeyPress, KeyRelease, ButtonPress and ButtonRelease events are ignored (with press events generating a call to bell). Then it acquires a local grab for $widget. The state of the widgets and the grab is restored by a call to $widget->Unbusy. π«Ά
π $widget->caret( ?-x => x?, ?-y => y?, ?-height => height? );
Sets and queries the caret location for the display of the specified Tk window window. The caret is the per-display cursor location used for indicating global focus (e.g. to comply with Microsoft Accessibility guidelines), as well as for location of the over-the-spot XIM (X Input Methods) or Windows IME windows. β¨
If no options are specified, the last values used for setting the caret are return in option-value pair format. -x and -y represent window-relative coordinates, and -height is the height of the current cursor location, or the height of the specified window if none is given.
π $widget->cells
Returns a decimal string giving the number of cells in the color map for $widget. π
π $widget->children
Returns a list containing all the children of $widget. The list is in stacking order, with the lowest window first. Top-level windows are returned as children of their logical parents. π
π $widget->class
Returns the class name for $widget. π
π $widget->colormapfull
Returns 1 if the colormap for $widget is known to be full, 0 otherwise. The colormap for a window is "known" to be full if the last attempt to allocate a new color on that window failed and this application hasn't freed any colors in the colormap since the failed allocation. no cap, that's the actual definition π₯
π $widget->ConfigSpecs
Used to perform delegated option configuration for a mega-widget. Returns, in Tk::Derived::ConfigSpecs notation (see Tk::ConfigSpecs), all possible options for a widget. For example:
$s = $self->Scale;
$self->ConfigSpecs(
$s->ConfigSpecs,
.... more ConfigSpecs specifications
);
returns a hash of all Tk::Scale options, delegated to $s β e.g. some representative examples:
-bigincrement => [$s, bigIncrement, BigIncrement, 0, 0]
-digits => [$s, digits, Digits, 0, 0]
-sliderlength => [$s, sliderLength, SliderLength, 10m, 30]
-troughcolor => [$s, troughColor, Background, #c3c3c3, #c3c3c3]
This provides an easy means of populating a mega-widget's ConfigSpecs with initializers. literally the glow-up hack π§
π $widget->containing(rootX,rootY)
Returns the window containing the point given by rootX and rootY. RootX and rootY are specified in screen units (i.e. any form acceptable to Tk_GetPixels) in the coordinate system of the root window (if a virtual-root window manager is in use then the coordinate system of the virtual root window is used). πΈ
If no window in this application contains the point then an empty string is returned. In selecting the containing window, children are given higher priority than parents and among siblings the highest one in the stacking order is chosen. π
π $widget->depth
Returns a decimal string giving the depth of $widget (number of bits per pixel). π¦
π $widget->destroy
This command deletes the window related to $widget, plus all its descendants. If all the MainWindows are deleted then the entire application will be destroyed. π
The perl object $widget continues to exist while references to it still exist, e.g. until variable goes out of scope. However any attempt to use Tk methods on the object will fail. Exists($widget) will return false on such objects. π
Note however that while a window exists for $widget the perl object is maintained (due to "references" in perl/Tk internals) even though original variables may have gone out of scope. (Normally this is intuitive.) π«Ά
π Exists($widget)
Returns 1 if there exists a window for $widget, 0 if no such window exists. vibe check: does it exist or not β
π $widget->font(option?, arg, arg, ...?)
Create and inspect fonts. See Tk::Font for further details. π
π $widget->fpixels(number)
Returns a floating-point value giving the number of pixels in $widget corresponding to the distance given by number. Number may be specified in any of the forms acceptable to Tk_GetScreenMM, such as "2.0c" or "1i". The return value may be fractional; for an integer value, use $widget->pixels. β¨
π $widget->Getimage(name)
Given name, look for an image file with that base name and return a Tk::Image. File extensions are tried in this order: xpm, gif, ppm, xbm until a valid iamge is found. If no image is found, try a builtin image with that name. π₯
π $widget->geometry
Returns the geometry for $widget, in the form widthxheight+x+y. All dimensions are in pixels. π
π $widget->height
Returns a decimal string giving $widget's height in pixels. When a window is first created its height will be 1 pixel; the height will eventually be changed by a geometry manager to fulfill the window's needs. π
If you need the true height immediately after creating a widget, invoke update to force the geometry manager to arrange it, or use $widget->reqheight to get the window's requested height instead of its actual height. iykyk π«Ά
π $widget->id
Returns a hexadecimal string giving a low-level platform-specific identifier for $widget. On Unix platforms, this is the X window identifier. Under Windows, this is the Windows HWND. On the Macintosh the value has no meaning outside Tk. π
π $widget->idletasks
One of two methods which are used to bring the application "up to date" by entering the event loop repeated until all pending events (including idle callbacks) have been processed. β
If the idletasks method is specified, then no new events or errors are processed; only idle callbacks are invoked. This causes operations that are normally deferred, such as display updates and window layout calculations, to be performed immediately. β¨
The idletasks command is useful in scripts where changes have been made to the application's state and you want those changes to appear on the display immediately, rather than waiting for the script to complete. Most display updates are performed as idle callbacks, so idletasks will cause them to run. However, there are some kinds of updates that only happen in response to events, such as those triggered by window size changes; these updates will not occur in idletasks. π
π $widget->interps
Returns a list whose members are the names of all Tcl interpreters (e.g. all Tk-based applications) currently registered for a particular display. The return value refers to the display of $widget. π¦
π $widget->ismapped
Returns 1 if $widget is currently mapped, 0 otherwise. β¨
π $widget->lower(?belowThis?)
If the belowThis argument is omitted then the command lowers $widget so that it is below all of its siblings in the stacking order (it will be obscured by any siblings that overlap it and will not obscure any siblings). πΈ
If belowThis is specified then it must be the path name of a window that is either a sibling of $widget or the descendant of a sibling of $widget. In this case the lower command will insert $widget into the stacking order just below belowThis (or the ancestor of belowThis that is a sibling of $widget); this could end up either raising or lowering $widget. plot twist, honestly π
$widget->MapWindowβ makes$widgetget "mapped", aka β¨visible on your displayβ¨. lowkey warning tho: this can confuse the geometry manager (pack,grid,place, ...) that thinks it's the one running the widget's life π$widget->managerβ returns the name of the geometry manager currently responsible for$widget, or an empty string if no geometry manager is managing it. that name is usually just the method name of the geometry manager, likepackorplace. but if the geometry manager is itself a widget, like canvases or text, then the name is that widget's class command, likecanvas. iykyk π«Ά$widget->nameβ returns$widget's name, meaning its name within its parent, not its whole full path name. and$mainwin->namehands you the name of the application itself π$widget->OnDestroy(callback);β takes a standard perl/Tk callback. when the window tied to$widgetis destroyed, the callback gets invoked. and here's the glow-up: unlike$widget->bind('<Destroy>',...), the widget's methods are still available when the callback runs, so (for example) a Text widget can save its contents to a file. OnDestroy was required for the newaftermechanism. periodt π$widget->parentβ returns$widget's parent, or an empty string if$widgetis the main window of the application.$widget->PathNameβ returns the Tk path name of$widget. this is the inverse of the"Widget"method. (it's an import from the C interface, fr fr.)$widget->pathname(id)β returns an object whose X identifier isid. that identifier gets looked up on$widget's display.idhas to be a decimal, hexadecimal, or octal integer and must correspond to a window in the invoking application, otherwise an error pops off β trap it witheval { }orTk::catch { }. if the window belongs to the application but isn't an object (wrapper windows, HList header, etc.) thenundefis what you get π$widget->pixels(number)β returns how many pixels in$widgetcorrespond to the distance given bynumber.numbercan be in any of the formsTk_GetPixelsaccepts, like`2.0c`or`1i`. the result gets rounded to the nearest integer value; want the fractional version instead? use$widget->fpixelsπ$widget->pointerxβ if the mouse pointer is on the same screen as$widget, returns the pointer's x coordinate, measured in pixels in the screen's root window. if a virtual root window is in use on the screen, the position is measured in the virtual root. if the mouse pointer isn't on the same screen as$widget, then-1comes back.$widget->pointerxyβ if the mouse pointer is on the same screen as$widget, returns a list with two elements: the pointer's x and y coordinates, measured in pixels in the screen's root window. if a virtual root window is in use on the screen, the position is computed in the virtual root. if the mouse pointer isn't on the same screen as$widget, then both of the returned coordinates are-1.$widget->pointeryβ if the mouse pointer is on the same screen as$widget, returns the pointer's y coordinate, measured in pixels in the screen's root window. if a virtual root window is in use on the screen, the position is computed in the virtual root. if the mouse pointer isn't on the same screen as$widget, then-1comes back.$widget->raise(?aboveThis?)β if you leave out theaboveThisargument, the command raises$widgetso it's above all of its siblings in the stacking order (it won't be obscured by any siblings, and it'll obscure any siblings that overlap it). ifaboveThisis specified, it must be the path name of a window that's either a sibling of$widgetor the descendant of a sibling of$widget. in that case, raise inserts$widgetinto the stacking order just aboveaboveThis(or the ancestor ofaboveThisthat is a sibling of$widget); and lowkey that could end up either raising or lowering$widgetπ$widget->reqheightβ returns a decimal string giving$widget's requested height, in pixels. this is the value$widget's geometry manager uses to compute its geometry.$widget->reqwidthβ returns a decimal string giving$widget's requested width, in pixels. this is the value$widget's geometry manager uses to compute its geometry.$widget->rgb(color)β returns a list containing three decimal values: the red, green, and blue intensities that correspond tocolorin the window given by$widget.colorcan be specified in any of the forms acceptable for a color option π$widget->rootxβ returns a decimal string giving the x-coordinate, in the root window of the screen, of the upper-left corner of$widget's border (or$widgetitself if it has no border).$widget->rootyβ returns a decimal string giving the y-coordinate, in the root window of the screen, of the upper-left corner of$widget's border (or$widgetitself if it has no border).$widget->scaling?(number)?β sets and queries the current scaling factor Tk uses to convert between physical units (points, inches, millimeters, that whole vibe) and pixels. thenumberargument is a floating point number specifying the number of pixels per point on$widget's display. ifnumberis omitted, you get back the current value of the scaling factor.- a "point" is a unit of measurement equal to 1/72 inch. a scaling factor of
1.0corresponds to 1 pixel per point, which is equivalent to a standard 72 dpi monitor. a scaling factor of1.25would mean 1.25 pixels per point, which is the setting for a 90 dpi monitor; setting the scaling factor to1.25on a 72 dpi monitor would make everything in the application display 1.25 times as large as normal. the initial value for the scaling factor gets set when the application starts, based on properties of the installed monitor (as reported via the window system), but it can be changed at any time. measurements made after the scaling factor is changed will use the new scaling factor, but it is undefined whether existing widgets will resize themselves dynamically to accommodate the new scaling factor. so likeβ¦ don't count on the glow-up π
- a "point" is a unit of measurement equal to 1/72 inch. a scaling factor of
$widget->screenβ returns the name of the screen associated with$widget, in the formdisplayName.screenIndex.$widget->screencellsβ returns a decimal string giving the number of cells in the default color map for$widget's screen.$widget->screendepthβ returns a decimal string giving the depth of the root window of$widget's screen (number of bits per pixel).$widget->screenheightβ returns a decimal string giving the height of$widget's screen, in pixels.$widget->screenmmheightβ returns a decimal string giving the height of$widget's screen, in millimeters.$widget->screenmmwidthβ returns a decimal string giving the width of$widget's screen, in millimeters.$widget->screenvisualβ returns one of the following strings to indicate the default visual class for$widget's screen:directcolor,grayscale,pseudocolor,staticcolor,staticgray, ortruecolor. pick your fighter β¨$widget->screenwidthβ returns a decimal string giving the width of$widget's screen, in pixels.$widget->serverβ returns a string with info about the server for$widget's display. the exact format of this string may vary from platform to platform. for X servers the string has the form`XmajorRminor vendor vendorVersion`wheremajorandminorare the version and revision numbers provided by the server (e.g.,X11R5),vendoris the name of the vendor for the server, andvendorReleaseis an integer release number provided by the server.$widget->toplevelβ returns the reference of the top-level window containing$widget. main character energy fr π$widget->Unbusyβ restores widget state after a call to$widget->Busy.$widget->UnmapWindowβ causes$widgetto be "unmapped", aka removed from the display. this does for any widget what$widget->withdrawdoes for toplevel widgets. and same warning as before: it may confuse the geometry manager (pack,grid,place, ...) that thinks it's managing the widget π$widget->updateβ one of two methods used to bring the application "up to date" by entering the event loop repeatedly until all pending events (including idle callbacks) have been processed.updateis useful in scripts where you're doing a long-running computation but you still want the application to respond to events like user interactions; if you callupdateoccasionally, user input will be processed during the next call toupdate. multitasking queen π«Ά
$widget->useinputmethods( ?boolean? )β sets and queries the state of whether Tk should use XIM (X Input Methods) for filtering events. the resulting state is returned. XIM is used in some locales (ie: Japanese, Korean) to handle special input devices. this feature is only significant on X. if XIM support isn't available, this will always return0. if thebooleanargument is omitted, the current state is returned. this is turned on by default for the main display πΈ$widget->viewableβ returns1if$widgetand all of its ancestors up through the nearest toplevel window are mapped. returns0if any of these windows aren't mapped. vibe check: passed or failed, no in between β$widget->visualβ returns one of the following strings to indicate the visual class for$widget:directcolor,grayscale,pseudocolor,staticcolor,staticgray, ortruecolor.$widget->visualidβ returns the X identifier for the visual for$widget.$widget->visualsavailable(?includeids?)β returns a list whose elements describe the visuals available for$widget's screen. each element consists of a visual class followed by an integer depth. the class has the same form as returned by$widget->visual. the depth gives the number of bits per pixel in the visual. plus, if theincludeidsargument is provided, then the depth is followed by the X identifier for the visual π¦$widget->vrootheightβ returns the height of the virtual root window associated with$widgetif there is one; otherwise it returns the height of$widget's screen.$widget->vrootwidthβ returns the width of the virtual root window associated with$widgetif there is one; otherwise it returns the width of$widget's screen.$widget->vrootxβ hands you the x-offset of the virtual root window that belongs to$widget, measured against the root window of its screen. it's normally zero or negative, no drama. and if$widgethas no virtual root window at all? you get0. that's it, that's the whole vibe. πΈ$widget->vrootyβ same energy but the y-offset this time, still relative to the root window of its screen, still zero or negative normally. and yeah,0again if there's no virtual root window for$widget. π$widget->waitVariable(\$name)$widget->waitVisibility$widget->waitWindowβ okay so these three are the tk wait methods and bestie they are so useful. the whole bit: they wait for one of several things to happen, then they come back without taking any other actions. the return value is always an empty string, every single time, no notes. πwaitVariablewants a reference to a perl variable, and then it justβ¦ waits for that variable to get modified. typically you use this to wait for a user to finish interacting with a dialog that sets the variable as part (possibly the final part) of the interaction. π«ΆwaitVisibilitywaits for a change in$widget's visibility state β as indicated by the arrival of aVisibilityNotifyevent. classic move: wait for a newly-created window to actually appear on screen before you do the next thing.waitWindowwaits for$widgetto be destroyed. and this one's the go-to when you want a user to finish interacting with a dialog box before you use the result of that interaction.
heads up: creating and destroying the window every time you need a dialog makes your code modular, yes, but it imposes overhead. you can dodge that by withdrawing the window instead and using waitVisibility. β¨
While the tk wait methods are waiting, they process events in the normal fashion, so your app keeps responding to user interactions like nothing's happening. but β and this is the important bit β if an event handler invokes tkwait again, the nested call to tkwait must complete before the outer call can complete. no cutting the line. π
$widget->Walk(proc?, arg, ...?)β traverse a widget hierarchy starting at$widgetwhile executing the subroutineprocon every visited widget. the argumentsarg, ...get supplied to that subroutine. it's giving little field trip through your widget tree. π$widget->Widget(pathname)β returns the widget reference for the given Tk path name, or"undef"if that path name doesn't match a Tk widget. this is the inverse of the"PathName"method. (it's an import from the C interface, iykyk.) π₯$widget->widthβ returns a decimal string giving$widget's width in pixels. now listen: when a window is first created its width will be1pixel. the width will eventually be changed by a geometry manager to fulfill the window's needs. so if you need the true width immediately after creating a widget, either invokeupdateto force the geometry manager to arrange it, or use$widget->reqwidthto get the window's requested width instead of its actual width. don't get caught lacking on this one. π$widget->windowingsystemβ returns the current Tk windowing system, and it's one ofx11(X11-based),win32(MS Windows),classic(Mac OS Classic), oraqua(Mac OS X Aqua). π$widget->xβ returns a decimal string giving the x-coordinate, in$widget's parent, of the upper-left corner of$widget's border (or$widgetitself if it has no border). β$widget->yβ returns a decimal string giving the y-coordinate, in$widget's parent, of the upper-left corner of$widget's border (or$widgetitself if it has no border). β¨
πΈ CAVEATS πΈ
okay so the above documentation on generic methods is incomplete. that's it. that's the caveat. we said what we said. π
π KEYWORDS π
atom, children, class, geometry, height, identifier, information, interpreters, mapped, parent, path name, screen, virtual root, width, window
and that's a wrap on part 3 bestie!! π this little page is from perl v5.40.1, dated 2025-04-13, and it lives as Widget(3pm) β go forth and widget responsibly, ily, mwah π
ππ¦
π¦ where this page comes from
- debian trixie: perl-tk 1:804.036+dfsg1-5
- ubuntu questing: perl-tk 1:804.036+dfsg1-5
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms