so I'm trying to open a tkinter window through sensing left clicks from the mouse using pyHook, and I want the newly opened window to get the focus. The problem is that no matter what focus method I try, the current window will always retain the focus instead of the focus switching to the new tkinter window. Here's the code:
from tkinter import *
import pyHook
import pythoncom
def open_GUI():
root = Tk()
root.title('test')
entry_box = Entry(root, font=("Calibri", 11))
entry_box.focus()
entry_box.pack(fill=X, side=RIGHT, expand=True)
root.after(1, lambda: root.focus_set())
root.mainloop()
return True
def MouseLeftDown_Func(event):
print('mouse')
open_GUI()
return True
def KeyDown_Func(event):
print('key')
return True
hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = KeyDown_Func
hooks_manager.MouseLeftDown = MouseLeftDown_Func
hooks_manager.HookKeyboard()
hooks_manager.HookMouse()
pythoncom.PumpMessages()
I think the issue is that when I left click on the current window the focus is prioritized to the window that was recently clicked (the current window), and any command calling for a tkinter window focus is ignored.
Does anyone know how I can switch the focus to the new tkinter window after the left click?