Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter main window focus

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?

like image 797
Python Spoils You Avatar asked Mar 30 '14 23:03

Python Spoils You


People also ask

What does focus () do tkinter?

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.

What does focus () do in Python?

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.

How do I close toplevel windows?

For a particular application, if we have defined a Toplevel window, then we can close it using the destroy() method.

How do I change the default window size in tkinter?

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.


1 Answers

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.

like image 190
Brian Fuller Avatar answered Sep 21 '22 14:09

Brian Fuller