Get current app window handle and bring window to front - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Get current app window handle and bring window to front (/thread-40996.html) |
Get current app window handle and bring window to front - rjdegraff42 - Oct-27-2023 I'm trying to set up a simple command line chat application between two computers on my home network. I have the server and client scripts working exactly the way I want except for one little thing. At least it should be little and I do not understand why it is so difficult. I intend to leave the script running in a small window. When it receives a new message I want it to pop to the front. When I comment out the line that does this the script works. But when I enable the "to front" line it aborts with I've tested the code for win32gui.SetForegroundWindow(hwnd) by starting two client apps and, from a python shell, running win32gui.SetForegroundWindow(hwnd) with (in turn) both of the handles. No problem. Until I enable the code in the client script. When I do that I get the exception. I am running Python 3.10.11 on Windows 11 Home.Here is the client script. import socket import threading import win32gui import ctypes host = 'jim-asus' port = 1234 hwnd = ctypes.windll.user32.GetForegroundWindow() server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.connect((host, port)) print(f'Connected to the server. {hwnd=}') def send_message(): while True: message = input('') server.send(message.encode()) def recv_message(): while True: message = server.recv(1024) print(">>", message.decode()) print(f'set {hwnd=} to front') win32gui.SetForegroundWindow(hwnd) send = threading.Thread(target=send_message) recv = threading.Thread(target=recv_message) send.start() recv.start() RE: Get current app window handle and bring window to front - Larz60+ - Oct-27-2023 Unless I am looking at the wrong win32gui, it looks like it hasn't been updated since 2017. Perhaps, for simple application, switch tkinter or some other gui? |