Widget & screen info#
Every widget carries a family of winfo_* methods that report information
about itself, its place in the widget tree, and the screen it lives on — its
class, size, position, geometry manager, the pointer location, screen
dimensions, and more. They are the read side of the widget: nothing here changes
state, so all of them are safe to call at any time.
Note
This is standard tkinter. The methods come from Tk’s winfo command, which
Tk documents only in a C-oriented manual page; this reference restates the
names and return types in Python terms. For the full list see the
tkinter module reference on python.org. For how
to use these — centering a window, reacting to a resize — see
Windows in the User Guide.
Identity & hierarchy#
Where a widget sits in the tree, what it is, and whether it is still alive.
Method |
Returns |
Meaning |
|---|---|---|
|
str |
The widget’s Tk class name ( |
|
str |
The widget’s own name within its parent (the last path component, e.g.
|
|
str |
The path name of the parent widget ( |
|
list |
The child widget objects, in stacking order. One level only — it does not recurse. |
|
widget |
The |
|
str |
The geometry manager currently managing the widget: |
|
int |
The platform window identifier (an |
|
str |
The widget path for a numeric window |
|
int |
|
|
int |
|
|
int |
|
Two more tree accessors are not winfo_* methods, but belong beside them:
Name |
Returns |
Meaning |
|---|---|---|
|
widget |
The parent widget object — the counterpart to |
|
dict |
The child widget objects keyed by their own names
( |
|
widget |
The widget object for a path name — turns |
Position & size#
Coordinates are in pixels. Position methods come in two flavors: relative to the
parent (x/y) and relative to the whole screen (rootx/rooty).
Method |
Returns |
Meaning |
|---|---|---|
winfo_x()winfo_y() |
int |
Top-left position relative to the parent. |
winfo_rootx()winfo_rooty() |
int |
Top-left position relative to the screen — the value to use when placing another window next to this one. |
winfo_width()winfo_height() |
int |
The widget’s current size. See the gotcha below. |
winfo_reqwidth()winfo_reqheight() |
int |
The size the widget requests from its geometry manager (its natural size before stretching). Valid immediately, without mapping. |
|
str |
Size and position as one |
Warning
Before a widget is drawn, winfo_width/winfo_height return 1 — the
real size is not known until the geometry manager has laid the widget out. If
you need the size right after creating a widget (for example to center a
window), force a layout pass first with update_idletasks:
app.update_idletasks()
w, h = app.winfo_width(), app.winfo_height()
Or read winfo_reqwidth/winfo_reqheight, which are valid immediately.
Pointer#
The mouse pointer position and what lies under it, independent of any event.
Method |
Returns |
Meaning |
|---|---|---|
winfo_pointerx()winfo_pointery() |
int |
Pointer position relative to the screen. |
|
tuple |
Both as a |
|
widget or None |
The widget under a screen coordinate, or |
Screen & display#
Facts about the display the widget is on — useful for sizing or centering a window against the monitor.
Method |
Returns |
Meaning |
|---|---|---|
winfo_screenwidth()winfo_screenheight() |
int |
The screen size in pixels. |
winfo_screenmmwidth()winfo_screenmmheight() |
int |
The screen size in millimeters — divide against the pixel size for the physical DPI. |
|
str |
The screen name ( |
|
str |
A string describing the windowing server / OS
( |
winfo_vrootwidth()winfo_vrootheight() |
int |
The size of the virtual root — the bounding box of all monitors on a multi-monitor desktop. |
winfo_vrootx()winfo_vrooty() |
int |
The virtual root’s offset from the screen origin. |
Unit & color conversion#
Helpers that convert Tk’s screen distances and color names using the widget’s own display, so results honor the actual DPI and colormap.
Method |
Returns |
Meaning |
|---|---|---|
|
int |
Convert a Tk distance — |
|
float |
The same conversion, unrounded. |
|
tuple |
A color name or |
Rarely needed#
Low-level display and X-server introspection — colormap depth, visual class, and X11 atoms. You will almost never call these from application code; they are listed for completeness.
Method |
Meaning |
|---|---|
winfo_depth()winfo_screendepth() |
Bits per pixel of the widget’s / screen’s visual. |
winfo_cells()winfo_screencells() |
Number of colormap cells. |
winfo_visual()winfo_screenvisual()winfo_visualid() |
The X visual class ( |
|
The list of visuals the screen supports. |
|
|
winfo_atom(name)winfo_atomname(id) |
Intern an X11 atom name to its integer id and back. |
|
The Tk interpreter names registered on the display. |
Example#
Center a window on the screen using screen size and the window’s requested size:
import ttkbootstrap as ttk
app = ttk.Window()
ttk.Label(app, text="Centered").pack(padx=40, pady=40)
app.update_idletasks() # settle the layout
w, h = app.winfo_width(), app.winfo_height()
x = (app.winfo_screenwidth() - w) // 2
y = (app.winfo_screenheight() - h) // 2
app.geometry(f"{w}x{h}+{x}+{y}")
app.mainloop()
Tip
ttkbootstrap already ships this: Window.place_window_center() does the
same thing (and stays correct across multiple monitors) — see
Windows. Reach for the raw
winfo_* methods when you need custom placement.