Skip to content

Commit a862fbc

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-123193: Document tkinter Variable lifetime (GH-152625) (GH-153098)
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 67d782c commit a862fbc

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
@@ -5736,6 +5739,14 @@ Variable classes
57365739
:class:`StringVar`, :class:`IntVar`, :class:`DoubleVar` or
57375740
:class:`BooleanVar` -- rather than :class:`!Variable` directly.
57385741

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

0 commit comments

Comments
 (0)