如何在所有打开的窗口中显示每个用户的特殊名称?

问题描述 投票:0回答:0

你好,所以我想遍历每个用户并查看是否打开了一个认证程序,对于我自己的用户,它使用这段代码,我在其中搜索名为“ExampleProgramm”的程序

    import tkinter as tk
    import win32api
    import win32gui
    import win32process
    import wmi
    
    def update_window_list():
        # Create a listbox to display the window list
        listbox = tk.Listbox(root, width=80)
        listbox.pack(padx=10, pady=10)
    
        # Define the callback function for EnumWindows
        def enum_windows_callback(hwnd, window_list):
            if win32gui.IsWindowVisible(hwnd):
                window_list.append((hwnd, win32gui.GetWindowText(hwnd)))
    
        # Get the list of open windows
        window_list = []
        win32gui.EnumWindows(enum_windows_callback, window_list)
    
        # Display the open windows in the listbox
        for hwnd, window_text in window_list:
            if window_text == "ExampleProgramm":
            # GetWindowThreadProcessId() verwenden, um den Prozess-ID zu erhalten
                tid, pid = win32process.GetWindowThreadProcessId(hwnd)
                
                listbox.insert(tk.END, f"{pid}: {window_text}")
    
    root = tk.Tk()
    root.geometry("500x400")
    
    # Schedule the window list update after the main loop starts
    root.after(0, update_window_list)
    
    root.mainloop()

但是当我尝试使用以下代码为每个用户更改它时,我没有得到任何输出:

    import tkinter as tk
import win32api
import win32gui
import win32process
import wmi

def enum_windows_callback(hwnd, window_list):
    if win32gui.IsWindowVisible(hwnd):
        window_list.append((hwnd, win32gui.GetWindowText(hwnd)))

def update_window_list():
    # Create a listbox to display the window list
    listbox = tk.Listbox(root, width=80)
    listbox.pack(padx=10, pady=10)

    # Get the list of open windows for all users
    window_list = []
    for process in wmi.WMI().Win32_Process():
        if process.Name == "ExampleProgramm":
            hwnd = win32gui.FindWindow(None, process.Name)
            if hwnd != 0:
                username = process.GetOwner().split("\\")[-1]
                window_list.append((hwnd, username, process.Name))

    # Display the open windows in the listbox with the corresponding username
    for hwnd, username, window_text in window_list:
        # GetWindowThreadProcessId() verwenden, um den Prozess-ID zu erhalten
        tid, pid = win32process.GetWindowThreadProcessId(hwnd)
        listbox.insert(tk.END, f"{username}: {pid}: {window_text}")

root = tk.Tk()
root.geometry("500x400")

# Schedule the window list update after the main loop starts
root.after(0, update_window_list)

root.mainloop()

我错过了什么,我怎样才能知道程序是否在不同的用户上运行

python python-3.x winapi
© www.soinside.com 2019 - 2024. All rights reserved.