[Python] python 3 - click tkinter button with pywinauto

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to press a tkinter button with pywinauto.

    import tkinter as tk
    from pywinauto import Application
    import time
    import threading


    # Add a button to the window
    def button_click():
    print("Button clicked!")


    def click_():
    time.sleep(1)
    app = Application(backend="uia").connect(title="Tkinter Example")
    window = app.window(title="Tkinter Example")
    print(window.print_ctrl_ids())
    window.child_window(title="Click Me").click_input()


    root = tk.Tk()
    root.title("Tkinter Example")
    button = tk.Button(root, text="Click Me", command=button_click)
    button.pack()

    threading.Thread(name='process1', target=click_).start()
    root.mainloop()


    It returns this error message

    pywinauto.findwindows.ElementNotFoundError: {'title': 'Click Me', 'top_level_only': False, 'parent': <uia_element_info.UIAElementInfo - 'Tkinter Example', TkTopLevel, 6491122>, 'backend': 'uia'}


    Additionally, it would be great if this automated click was able to execute while the tkinter root window is minimized to the windows toolbar. And a click that does not move the cursor.

    Is this possible?

    I have also included the controller ids in a print function in the example code.

    print(window.print_ctrl_ids())

    Continue reading...

Compartilhe esta Página