I have the following code
window = Tk()
window.lift()
window.attributes("-topmost", True)
This code works in that it displays my Tkinter window above all other windows, but it still only solves half of the problem. While the window is in fact displayed above all other windows, the window does not have focus. Is there a way not only to make the window the frontmost window in Tkinter, but also put focus on it?
To manage and give focus to a particular widget, we generally use the focus_set() method. It focuses the widget and makes them active until the termination of the program.
Focus is used to refer to the widget or window which is currently accepting input. Widgets can be used to restrict the use of mouse movement, grab focus, and keystrokes out of the bounds. However, if we want to focus a widget so that it gets activated for input, then we can use focus. set() method.
For a particular application, if we have defined a Toplevel window, then we can close it using the destroy() method.
Syntax – geometry() To set a specific size to the window when using Python tkinter, use geometry() function on the Tk() class variable. where width and height should be replaced with integers that represent the width and height of the window respectively.
If focus_force()
is not working you can try doing:
window.after(1, lambda: window.focus_force())
It's essentially the same thing, just written differently. I just tested it on python 2.7.
root.focus_force()
wouldn't work but the above method did.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With