Skip to content

Conversation

@mdehoon
Copy link
Contributor

@mdehoon mdehoon commented Oct 15, 2025

When using tkinter in an interactive Python session, the EventHook function in Modules/_tkinter.c runs the Tcl/Tk event loop, and exits once data becomes available on stdin (i.e., when the user types in the next Python command).

The EventHook function monitors for input on stdin using Tcl_CreateFileHandler.

As Tcl_CreateFileHandler is Unix-only, for Windows instead the _kbhit function from Microsoft Windows is used.

However, this prevents stdin from being used with a pipe (see Issue #140146 ), as _kbhit checks for a keystroke on an actual keyboard.

This PR replaces Tcl_CreateFileHandler by Tcl_CreateChannelHandler and Tcl_GetStdChannel(TCL_STDIN), which do not depend on platform.

This has two advantages:

  1. It allows stdin to be used as a pipe on all platforms, which is useful for testing interactive usage of tkinter;
  2. We can get rid of some Windows-specific code in Modules/_tkinter.c.

@mdehoon
Copy link
Contributor Author

mdehoon commented Nov 8, 2025

Unfortunately this still doesn't quite work on Windows. The callback function MyChannelProc gets called when data is available on stdin, but it seems that data is already read from stdin into the channel's internal buffer before the callback is called. If we then exit the Tcl_DoOneEvent loop return from EventHook, Python tries to read the data from stdin, but won't find anything there.

See this ticket on the Tcl website for more information: https://core.tcl-lang.org/tcl/tktview/1d06180006

@mdehoon mdehoon marked this pull request as draft November 8, 2025 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant