杰瑞科技汇

Python winguiauto如何实现UI自动化?

Of course! Let's dive into Winguiauto.

What is Winguiauto?

Winguiauto is a Python library for GUI automation on Windows. Its name is a combination of "Win" (Windows) and "GUI" (Graphical User Interface) + "auto" (automation).

Think of it as a tool that lets your Python script "see" and "interact" with the windows and controls of other applications on your Windows desktop. It can do things like:

  • Find a window (e.g., "Notepad", "Calculator").
  • Read text from a window or a specific button.
  • Click buttons, checkboxes, or list items.
  • Type text into input fields.
  • Take a screenshot of a window or the entire desktop.
  • Check if a window is visible or minimized.

How does it work? (The Core Concept)

Winguiauto works by interacting with the UI elements of applications. It identifies these elements (buttons, text boxes, etc.) using a set of properties, most importantly:

  1. Control Identifier (CtrlID): A unique number for each control within a window. This is the most reliable way to target a specific element.
  2. Class Name: The internal Windows class of the control (e.g., "Edit" for a text box, "Button" for a button).
  3. Window Title/Control Text: The text displayed on the window or control (e.g., the title "Calculator" or the button text "OK").

The most common workflow is:

  1. Find a Window: Get a handle to the main window you want to automate.
  2. Find a Control: Inside that window, find the specific control you want to interact with (e.g., the "2" button in the Calculator).
  3. Perform an Action: Call a method on that control (e.g., click()).

Installation

It's a simple pip installation. Open your command prompt or terminal and run:

pip install winguiauto

Key Classes and Methods

The library revolves around two main classes: App and Control.

App Class

Represents an application window.

  • App(title=None, class_name=None): Constructor to find an application window.
    • title: The title of the window (e.g., "Calculator").
    • class_name: The Windows class name of the window.
  • window: A property that returns the main window Control object.
  • get_controls(): Returns a list of all Control objects within the window.

Control Class

Represents a single UI element (a control) like a button, text box, etc.

  • click(): Simulates a mouse click.
  • set_text(text): Sets the text of a control (e.g., a text box).
  • get_text(): Gets the text from a control.
  • get_value(): Gets the value of a control (e.g., the current number in a calculator display).
  • is_checked(): Checks if a checkbox is ticked.
  • set_checked(state): Sets the checked state of a checkbox.
  • exists(): Returns True if the control still exists.
  • visible_rect: A property that gives the (left, top, right, bottom) coordinates of the control on the screen.

Practical Examples

Let's walk through a few common automation tasks.

Example 1: Automating the Windows Calculator

This is the classic "hello world" of GUI automation. We will open the calculator, type "123 + 45 =", and read the result.

import winguiauto as win
import time
# 1. Launch the Calculator application
# You can use subprocess to launch it
import subprocess
subprocess.Popen("calc.exe")
# 2. Find the Calculator window
# We'll use the window title. It might take a moment to appear.
# We can add a small delay or use a loop to wait for it.
time.sleep(2) # Wait for the app to open
calc = win.App(title="Calculator")
# 3. Find the controls (buttons and display)
# It's often easier to find controls by their text.
# We can use the `get_controls()` method and filter by text.
def find_control_by_text(parent_control, text):
    """Helper function to find a control by its text."""
    for ctrl in parent_control.get_controls():
        if ctrl.get_text() == text:
            return ctrl
    return None
# 4. Interact with the controls
# Find the '1' button and click it
btn_1 = find_control_by_text(calc.window, "1")
btn_1.click()
# Find the '+' button and click it
btn_plus = find_control_by_text(calc.window, "+")
btn_plus.click()
# Find the '2' button and click it
btn_2 = find_control_by_text(calc.window, "2")
btn_2.click()
# Find the '=' button and click it
btn_equals = find_control_by_text(calc.window, "=")
btn_equals.click()
# 5. Get the result from the display
# The display control usually has no text but a value.
# We'll loop through controls to find the one with a value.
result_control = None
for ctrl in calc.window.get_controls():
    # The display often has a value and is an 'Edit' or 'Static' control
    if ctrl.get_value() is not None:
        result_control = ctrl
        break
if result_control:
    print(f"The result is: {result_control.get_value()}")
else:
    print("Could not find the result display.")
# Expected Output: The result is: 123

Example 2: Automating Notepad

This example shows how to open Notepad, type a sentence, and save the file.

import winguiauto as win
import subprocess
import time
# 1. Launch Notepad
subprocess.Popen("notepad.exe")
time.sleep(2) # Wait for Notepad to open
# 2. Find the Notepad window
notepad = win.App(class_name="Notepad") # Using class name is often more stable
# 3. Type text into the editor
# The main text area in Notepad is usually a 'Edit' control.
# We can find it by its class name.
text_area = None
for ctrl in notepad.window.get_controls():
    if ctrl.class_name == "Edit":
        text_area = ctrl
        break
if text_area:
    text_area.set_text("Hello from Winguiauto! This is a test.")
    print("Text typed into Notepad.")
else:
    print("Could not find the text area in Notepad.")
# 4. Save the file
# We can simulate File -> Save
# First, find the "File" menu item and click it
file_menu = None
for ctrl in notepad.window.get_controls():
    if ctrl.get_text() == "File":
        file_menu = ctrl
        break
if file_menu:
    file_menu.click()
    time.sleep(0.5) # Wait for menu to appear
    # Now find the "Save..." menu item in the popup menu
    # This requires finding the menu window
    save_menu = None
    # We can look for a window with class "#32768" which is a common menu class
    menu_win = win.App(class_name="#32768")
    if menu_win:
        save_item = find_control_by_text(menu_win.window, "Save...")
        if save_item:
            save_item.click()
            time.sleep(1)
            # The "Save As" dialog appears. We need to interact with it.
            # Find the "File name:" edit box and type a name
            save_dialog = win.App(title="Save As")
            file_name_box = None
            for ctrl in save_dialog.window.get_controls():
                if ctrl.get_text() == "File name:":
                    # The actual edit box is usually the next control
                    file_name_box = ctrl.parent.get_controls()[1] # A bit heuristic
                    break
            if file_name_box:
                file_name_box.set_text("test_winguiauto.txt")
                # Find and click the "Save" button
                save_btn = find_control_by_text(save_dialog.window, "Save")
                if save_btn:
                    save_btn.click()
                    print("File saved as 'test_winguiauto.txt'.")

Important Considerations and Best Practices

  1. Stability is Key: UI automation can be brittle. A small UI update (like changing a button's text) can break your script.

    • Use CtrlID whenever possible: This is the most reliable way to identify a control, as it doesn't change with text or UI updates. You can find it using tools like AutoIt Window Info (which is part of the AutoIt suite) or Inspect.exe (a Windows SDK tool).
    • Use class_name: More stable than text.
    • Use title or text as a last resort or in combination with other properties.
  2. Add Delays: Applications take time to load, windows take time to appear, and menus take time to open. Using time.sleep() is a simple but effective way to make your script more robust. For more advanced waiting, you can create a loop that checks for a control's existence before proceeding.

  3. Error Handling: Always wrap your code in try...except blocks. A control might not be found, or a window might not exist. Handle these cases gracefully.

  4. Permissions: Your script needs to have the necessary permissions to access the target application's windows. Running your script as an administrator might be required for some applications.

Comparison with Other Libraries

  • PyWin32 / win32gui: More powerful and lower-level. It gives you direct access to the Windows API. It's more complex to use but offers maximum control. Winguiauto can be seen as a higher-level, more user-friendly wrapper around some of these concepts.
  • PyAutoGUI: Cross-platform (Windows, macOS, Linux). It works by simulating mouse movements and keyboard keystrokes based on screen coordinates (x, y). It's great for simple "click here, type there" automation but can't directly interact with UI elements by their properties (like CtrlID). It's less reliable if the screen resolution or window positions change.
  • pywinauto: This is the most powerful and comprehensive GUI automation library for Windows. It's very robust and can handle complex applications. It has a steeper learning curve than Winguiauto but is generally the preferred choice for serious, large-scale automation projects.

In summary: Winguiauto is a great, easy-to-start library for Windows GUI automation. For simple to moderate tasks, it's perfect. If you find yourself needing more power or running into limitations, pywinauto is the natural next step.

分享:
扫描分享到社交APP
上一篇
下一篇