Skip to content

tkinter: return str and numbers for more Tcl object types #153513

Description

@serhiy-storchaka

When wantobjects is enabled (the default), _tkinter converts values returned from Tcl/Tk to the equivalent Python type where one exists (bool, int, float, bytes, str, tuple) and wraps everything else in an opaque _tkinter.Tcl_Obj. Several common Tcl object types are still returned as Tcl_Obj even though a natural Python type exists, which forces callers to wrap results in str(), getint(), etc.

Proposal: convert a few more object types in FromObj().

str for types whose internal representation is only a throwaway, context-bound lookup with nothing worth preserving for a round-trip back to Tcl:

  • index (an enumeration keyword, e.g. the value of -orient or -state)
  • window (e.g. the value of -labelwidget)
  • nsName (a namespace name)
  • parsedVarName (e.g. the value of -variable)

An index value is always one of a small fixed set of keywords, so the resulting str is interned — equal keywords share a single object, saving memory and speeding up == and dict lookups.

int/float for the pixel type (a screen distance): a value with no unit suffix is already in pixels and screen-independent, so it is returned as an int or float. A value with an m/c/i/p suffix depends on the screen resolution and needs a Tk_Window to resolve, which is not available in FromObj(), so it is kept as a Tcl_Obj.

Deliberately left as Tcl_Obj:

  • color, border, font, cursor — their internal representation caches an allocated X/font resource that Tk reuses when the object is passed back, so wrapping preserves a real performance benefit.
  • dict — a structured value, best consumed with _splitdict() rather than flattened to a str.

Notes / compatibility:

  • Only affects the object mode (wantobjects); with objects disabled everything is already str.
  • The set of types that actually surface is identical across Tcl/Tk 8.6, 8.7, 9.0 and 9.1 (verified). The only cross-version difference is that classic-widget cget on Tk < 9 eagerly resolves a unit-bearing screen distance to an integer pixel count, so the "unit-bearing distance stays a Tcl_Obj" behavior is Tk 9.0+ only; and the nsName object type exists only on Tk 9.0+ (on 8.x namespace current is a plain string — still str).

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions