How to prevent Tkinter root window from taking over focus?

Viewed 247

I'm writing a program where most of user interaction happens in Windows Command line, however I use Tkinter for some File and Directory selection dialogs.

When I start up program by clicking .py file (In IDLE the problem doesn't occur), command line is launched and when Tk root window is instantiated, it takes over the focus from command line, even if I withdraw it. This behaviour requires extra action from user to focus back on command line window.

Here is some code sample to reproduce the problem.

from tkinter import Tk
root_window = Tk()
root_window.withdraw()
name = input("Enter your name:\n")
print("Nice to meet you, " + name)

How do I keep Windows Command line in focus?

1 Answers

Strange enough, now your example works for me in the intended way - even though, ten minutes ago, it showed the problem you have reported. The only other thing I tried out inbetween was to add a latency to the withdraw call:

root_window.after(1, lambda: root_window.withdraw())

It seemed to also work as intended with a latency argument of zero, but now I am not sure what exactly changed the behaviour.

Related