Skip to content

Add tkinter support for Tcl channels #162537

Backlog

Description

@serhiy-storchaka

tkinter can register a callback to run from the Tk mainloop when a file descriptor becomes readable or writable, via tk.createfilehandler() / tk.deletefilehandler(). But these wrap Tcl_CreateFileHandler, which is not implemented on Windows, so there is no portable way to do asynchronous I/O on a file, pipe or socket inside the Tk event loop.

Tcl's channel API (Tcl_MakeFileChannel, Tcl_CreateChannelHandler) works on all platforms. I propose exposing it in tkinter.

A new method creates a channel from a file, a pipe or a socket:

channel = widget.tk.createchannel(file, mode='r')
  • file is an object with a fileno() method (a file or socket) or an integer file descriptor.
  • mode is 'r', 'w' or 'rw'.
  • The channel duplicates the descriptor/handle/socket, so closing the channel does not close the original, and vice versa.
  • The channel is binary and non-blocking.

It returns a tkchannel object:

  • read(size=-1) — read up to size bytes; b'' at end of file, None if nothing is available.
  • write(data) — write a bytes-like object.
  • flush(), eof(), close().
  • createhandler(mask, func) / deletehandler() — call func(channel, mask) when the channel becomes readable or writable, where mask ORs tkinter.READABLE and tkinter.WRITABLE.
  • name — the Tcl channel name, usable from Tcl commands and options that take a channel, such as the channel option of Canvas.postscript().
  • closed.

It is also a context manager that closes the channel on exit.

This complements the existing file handlers (which stay as the thin Unix-only wrapper) and overlaps in spirit with gh-154801 (exposing more of Tcl's event-loop API).

Linked PRs

Activity

  1. added
    type-feature
    on Nov 24, 2026
  2. added
    extension-modules
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulestopic-tkintertype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions