You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过pywinauto控制同程序不同PID的多股票交易软件窗口

Controlling Multiple Stock Trading App Instances with pywinauto via PID

Got it, let's walk through exactly how to control separate instances of your stock trading software using their unique PIDs with pywinauto. This is a common scenario when managing multiple accounts, so let's break it down into actionable steps:

1. Get the PIDs of Your App Instances

First, you need to identify the PID for each running instance. You can do this two ways:

  • Manually: Open Task Manager (Ctrl+Shift+Esc), go to the Details tab, and find the process names for your stock trading app—each instance will have its own PID.
  • Automatically with pywinauto: If you want to script this, use find_elements to filter windows matching your app's criteria:
    from pywinauto.findwindows import find_elements
    
    # Replace with your app's process name or title pattern
    target_windows = find_elements(process_name="YourStockApp.exe", title_re=".*交易窗口.*")
    for win in target_windows:
        print(f"Instance Title: {win.title}, PID: {win.process_id}")
    
    This will output all matching windows along with their PIDs, so you can capture them for later use.

2. Connect to Each Instance Using PID

Once you have the PIDs, use pywinauto's Application class to connect to each instance individually. Make sure to choose the right backend (uia or win32—test both if you're unsure which works for your app):

from pywinauto import Application

# Replace with your actual PIDs
pid1 = 12345
pid2 = 67890

# Connect to each instance
app1 = Application(backend="uia").connect(process=pid1)
app2 = Application(backend="uia").connect(process=pid2)

3. Access the Main Window for Each Instance

Next, grab the main window object for each connected app. You can use top_window() to get the active window, or filter by title/class name if you need more precision:

# Option 1: Grab the top window of each instance
win1 = app1.top_window()
win2 = app2.top_window()

# Option 2: Filter by title (if your app's window title includes account info)
win1 = app1.window(title_re=".*账号12345.*")
win2 = app2.window(title_re=".*账号67890.*")

Pro tip: If a window is minimized, restore it before interacting:

if not win1.is_visible():
    win1.restore()

4. Control Each Window Independently

Now you can interact with each window separately—simulate clicks, input text, or perform any actions you need:

# Example: Click the "Buy" button on the first instance's window
win1.button(name="买入").click()

# Example: Enter a stock code in the second instance's input box
win2.edit(name="股票代码").set_text("600000")

Key Notes

  • Backend Compatibility: Some apps work better with win32 backend (for older Win32 apps) while others need uia (for modern apps using WPF/WinForms). Test both if you run into issues.
  • Window Identification: If your app's windows have unique titles (e.g., including the account number), using title_re can be more reliable than relying solely on PID (though PID is still solid).
  • Avoid Race Conditions: If you're launching instances programmatically, add a short delay (time.sleep(2)) before connecting to ensure the window is fully loaded.

内容的提问来源于stack exchange,提问作者user8283456

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:47:19