Tkinter main focus window

I have the following code

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

This code works in that it displays my Tkinter window over all other windows, but it only solves half the problem. Although the window is actually displayed above all other windows, the window has no focus. Is there a way to not only make the window the front-most window in Tkinter, but also focus on it?

+6
source share
4 answers

If focus_force() does not work, you can try:

 window.after(1, lambda: window.focus_force()) 

It is essentially the same, just written in different ways. I just tested it on python 2.7.

root.focus_force() will not work, but this method did.

+4
source

The solution for Windows is a little more complicated - you cannot just steal focus from another window, you must somehow bring the application to the forefront. But first, I suggest you use a small bit of window theory , so we can confirm that this is what we want to achieve.

As I mentioned in comment , this is a good opportunity to use SetForegroundWindow (also check for restrictions!). But consider things like cheap hacking, as the user “owns” the foreground, and Windows will try to stop you at all costs:

An application cannot force a window in the foreground while the user is working with another window. Instead, Windows flashes a window taskbar button to notify the user.

Also pay attention to this page :

The system automatically resolves calls to SetForegroundWindow if the user presses the ALT key or takes some actions, due to which the system itself changes the foreground window (for example, by clicking the background window).

Here is the simplest solution, since we can emulate Alt press:

 import tkinter as tk import ctypes # store some stuff for win api interaction set_to_foreground = ctypes.windll.user32.SetForegroundWindow keybd_event = ctypes.windll.user32.keybd_event alt_key = 0x12 extended_key = 0x0001 key_up = 0x0002 def steal_focus(): keybd_event(alt_key, 0, extended_key | 0, 0) set_to_foreground(window.winfo_id()) keybd_event(alt_key, 0, extended_key | key_up, 0) entry.focus_set() window = tk.Tk() entry = tk.Entry(window) entry.pack() # after 2 seconds focus will be stolen window.after(2000, steal_focus) window.mainloop() 

Some links and examples:

+1
source

Note. It depends on Windows. This focuses the main window. Essentially for going to the alt-tab window.


None of these answers worked for me, the tk window would appear on top with the -topmost flag, my input field would have focus, but the window itself would not, and that means t would appear in the tk window. It looks like this:

enter image description here

What helped add the Windows API call to steal focus:

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

Disclaimer: focus theft is bad behavior, so use it if necessary, and if that makes sense, for example. input field that the user instantly enters.

0
source

Not sure what problems you are facing. But below is an example code that works great for me.

 from tkinter import * window = Tk() def handle_focus(event): if event.widget == window: window.focus_set() input1.focus_set() label1 = Label(window,text = "Enter Text 2") input1 = Entry(window, bd=5) label2 = Label(window,text = "Enter Text 2") input2 = Entry(window, bd=5) submit = Button(window, text="Submit") label1.pack() input1.pack() label2.pack() input2.pack() submit.pack(side=BOTTOM) window.lift() window.attributes("-topmost", True) window.bind("<FocusIn>", handle_focus) hwnd = window.winfo_id() window.mainloop() 

This has been tested using the latest version of Python 3.6 on Windows 10.

Test results

As a result, it was found that after starting the program, I could just start typing, and it will go into the first text field

-1
source

All Articles