2

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?

hailsatyr
  • 23
  • 3
  • 1
    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. – Hans Aug 24 '20 at 11:13
  • Why are you using tkinter here? Maybe there might be a way to get around from there – Delrius Euphoria Aug 24 '20 at 11:30
  • AFAIK tkinter doesn't handle this you would need to work with an additional library that keeps track of it, like ctypes. But why don't you use a entry? https://stackoverflow.com/q/30512267 – Thingamabobs Aug 24 '20 at 11:35
  • Try saying `root.attributes('-topmost',False)` be also sure to say `root.mainloop()` and let me know – Delrius Euphoria Aug 24 '20 at 11:37
  • @Hans, I don't know how, but 10ms delay seemed to do the trick for me. Thanks! – hailsatyr Aug 25 '20 at 01:31
  • @hailsatyr, thanks for the feedback - I have pasted my comment into an answer. Feel free to accept it, if you like :) – Hans Aug 25 '20 at 07:14

3 Answers3

1

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.

Hans
  • 2,419
  • 2
  • 30
  • 37
0

Not a timing issue as such, I believe, but it looks like the 'input' command must be be issued before the root_window.withdraw completes. The code snippet above delays the withdraw command by 1 second, and sleeps for 0.9s before issuing the input command. It does not lose focus, (at least on my PC, using python 3.11.3) If you increase the sleep to 1.1s the focus is passed to the tkinter window.

Mike
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 12 '23 at 06:09
0
    import time
    import tkinter

    root=tkinter.Tk()
    root.after(1000, lambda:root.withdraw()) # delay 1000ms
    time.sleep(0.9)   # increase this to 1.1s and you lose focus
    name=input("Enter your name:")
    root.deiconify()
    tkinter.mainloop()

exit()
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
Mike
  • 1
  • 1
  • Please avoid code only answer and provide an explanation. You also could have a look at [how to answer](https://stackoverflow.com/help/how-to-answer). – doneforaiur Aug 27 '23 at 06:51