Skip to content

Commit aa9a3cd

Browse files
serhiy-storchakaclaude
authored andcommitted
gh-123193: Document tkinter Variable lifetime (GH-152625)
A Tk variable wrapper unsets its Tcl variable when garbage collected, so a reference must be kept while a widget uses it. Otherwise Tk recreates the Tcl variable but never unsets it again, leaking it. (cherry picked from commit 820b6ca) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent f7eda8e commit aa9a3cd

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

Doc/library/tkinter.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ method on it, and to change its value you call the :meth:`!set` method.
656656
If you follow this protocol, the widget will always track the value of the
657657
variable, with no further intervention on your part.
658658

659+
Keep a reference to the variable for as long as a widget uses it, for example
660+
by storing it as an attribute (see :class:`Variable`).
661+
659662
For example::
660663

661664
import tkinter as tk
@@ -5733,6 +5736,14 @@ Variable classes
57335736
:class:`StringVar`, :class:`IntVar`, :class:`DoubleVar` or
57345737
:class:`BooleanVar` -- rather than :class:`!Variable` directly.
57355738

5739+
.. note::
5740+
5741+
When a :class:`!Variable` is garbage collected, its Tcl variable is unset.
5742+
Keep a reference to it for as long as a widget is linked to it, for example
5743+
by storing it as an attribute rather than in a local variable.
5744+
Otherwise Tk recreates the Tcl variable to keep the widget working, but it
5745+
is never unset again, leaking one Tcl variable per dropped wrapper.
5746+
57365747
.. versionchanged:: 3.10
57375748
Two variables now compare equal (``==``) only when they have the same
57385749
name, are of the same class, and belong to the same Tcl interpreter.

0 commit comments

Comments
 (0)