Tkinter main window focus

后端 未结 6 659
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 10:36

I have the following code

window = Tk()
window.lift()
window.attributes(\"-topmost\", True)

This co

6条回答
  •  庸人自扰
    2020-12-09 10:46

    Note: this is Windows specific. This focuses the whole main window. Essentially forcing an alt-tab to reach the window.


    None of these answers worked for me, the tk window would appear on top of everything with the -topmost flag, my input box would have focus, but the window itself wouldn't, which meant that typing wouldn't appear in the tk window. It looks like like this:

    What helped was adding a call to the Windows API to steal focus:

    import win32gui    
    root = tk.Tk()
    
    # Application specific setup ...
    
    win32gui.SetForegroundWindow(root.winfo_id())
    

    Disclaimer: Stealing focus is bad behaviour, so only use it if you must and if it makes sense, e.g. input box the user will instantly type into.

提交回复
热议问题