Sep-28-2023, 03:46 PM
Hi guys, I am trying to open a popup window when the user types something.
I read somewhere that Windows only allows you to focus a popup window if it has been called form the main window.
I search in the web and found this "work around", not very robust, because the methods to force focus were not working:
Is there any library or windows api I can use to send my tkinter window to foreground, always on top and focused, withou calling it from the main window app, that will be minimized?
And one more question? How do I show this popup exactly on the same place or closer to the text cursor? I am using the code below, but is not working. Thanks in advance.
I read somewhere that Windows only allows you to focus a popup window if it has been called form the main window.
I search in the web and found this "work around", not very robust, because the methods to force focus were not working:
def bring_window_to_foreground(self): self.top_window.update_idletasks() hwnd = win32gui.FindWindow(None, "Select Expansion") shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys("%") time.sleep(0.1) shell.SendKeys("%") win32gui.SetForegroundWindow(hwnd) # Send another Alt key to nullify the activation self.top_window.focus_force()But sometimes this wont work and apps the use "alt" key to show menus like notepad will have problemas sometimes.
Is there any library or windows api I can use to send my tkinter window to foreground, always on top and focused, withou calling it from the main window app, that will be minimized?
And one more question? How do I show this popup exactly on the same place or closer to the text cursor? I am using the code below, but is not working. Thanks in advance.
def get_caret_position(self): class GUITHREADINFO(ctypes.Structure): _fields_ = [("cbSize", ctypes.c_ulong), ("flags", ctypes.c_ulong), ("hwndActive", ctypes.wintypes.HWND), ("hwndFocus", ctypes.wintypes.HWND), ("hwndCapture", ctypes.wintypes.HWND), ("hwndMenuOwner", ctypes.wintypes.HWND), ("hwndMoveSize", ctypes.wintypes.HWND), ("hwndCaret", ctypes.wintypes.HWND), ("rcCaret", ctypes.wintypes.RECT)] guiThreadInfo = GUITHREADINFO(cbSize=ctypes.sizeof(GUITHREADINFO)) hwnd = win32gui.GetForegroundWindow() processID = ctypes.c_ulong() threadID = ctypes.windll.user32.GetWindowThreadProcessId(hwnd, ctypes.byref(processID)) # <-- Corrected line if ctypes.windll.user32.GetGUIThreadInfo(threadID, ctypes.byref(guiThreadInfo)): caret_x = guiThreadInfo.rcCaret.left caret_y = guiThreadInfo.rcCaret.top return caret_x, caret_y else: return None, None