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 $widget get "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, like pack or place. but if the geometry manager is itself a widget, like canvases or text, then the name is that widget's class command, like canvas. iykyk 🫢
  • $widget->name β€” returns $widget's name, meaning its name within its parent, not its whole full path name. and $mainwin->name hands you the name of the application itself πŸ‘‘
  • $widget->OnDestroy(callback); β€” takes a standard perl/Tk callback. when the window tied to $widget is 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 new after mechanism. periodt πŸ’…
  • $widget->parent β€” returns $widget's parent, or an empty string if $widget is 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 is id. that identifier gets looked up on $widget's display. id has 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 with eval { } or Tk::catch { }. if the window belongs to the application but isn't an object (wrapper windows, HList header, etc.) then undef is what you get πŸ™ˆ
  • $widget->pixels(number) β€” returns how many pixels in $widget correspond to the distance given by number. number can be in any of the forms Tk_GetPixels accepts, 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 -1 comes 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 -1 comes back.
  • $widget->raise(?aboveThis?) β€” if you leave out the aboveThis argument, the command raises $widget so 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). if aboveThis is specified, it must be the path name of a window that's either a sibling of $widget or the descendant of a sibling of $widget. in that case, raise inserts $widget into the stacking order just above aboveThis (or the ancestor of aboveThis that 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 to color in the window given by $widget. color can 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 $widget itself 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 $widget itself 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. the number argument is a floating point number specifying the number of pixels per point on $widget's display. if number is 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.0 corresponds to 1 pixel per point, which is equivalent to a standard 72 dpi monitor. a scaling factor of 1.25 would mean 1.25 pixels per point, which is the setting for a 90 dpi monitor; setting the scaling factor to 1.25 on 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 πŸ’€
  • $widget->screen β€” returns the name of the screen associated with $widget, in the form displayName.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, or truecolor. 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` where major and minor are the version and revision numbers provided by the server (e.g., X11R5), vendor is the name of the vendor for the server, and vendorRelease is 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 $widget to be "unmapped", aka removed from the display. this does for any widget what $widget->withdraw does 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.
    • update is 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 call update occasionally, user input will be processed during the next call to update. 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 return 0. if the boolean argument is omitted, the current state is returned. this is turned on by default for the main display 🌸
  • $widget->viewable β€” returns 1 if $widget and all of its ancestors up through the nearest toplevel window are mapped. returns 0 if 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, or truecolor.
  • $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 the includeids argument 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 $widget if there is one; otherwise it returns the height of $widget's screen.
  • $widget->vrootwidth β€” returns the width of the virtual root window associated with $widget if 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 $widget has no virtual root window at all? you get 0. 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, 0 again 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. πŸ’…
    • waitVariable wants 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. 🫢
    • waitVisibility waits for a change in $widget's visibility state β€” as indicated by the arrival of a VisibilityNotify event. classic move: wait for a newly-created window to actually appear on screen before you do the next thing.
    • waitWindow waits for $widget to 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 $widget while executing the subroutine proc on every visited widget. the arguments arg, ... 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 be 1 pixel. 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 invoke update to force the geometry manager to arrange it, or use $widget->reqwidth to 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 of x11 (X11-based), win32 (MS Windows), classic (Mac OS Classic), or aqua (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 $widget itself 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 $widget itself 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