pythongui监控window窗体

pythongui监控window窗体

效果:
在这里插入图片描述

PIL实现

import tkinter as tk
from PIL import  ImageTk
import pygetwindow as gw
import pyautogui

# Define the window title
window_title = "Plants vs. Zombies"


# Function to update the image in the tkinter window
def update_image():
    if windows:
        # Get the first matching window
        window = windows[0]

        # Get the window's coordinates and size
        left, top, width, height = window.left, window.top, window.width, window.height

        # Capture the window's content
        screenshot = pyautogui.screenshot(region=(left, top, width, height))

        # Convert to ImageTk
        img_tk = ImageTk.PhotoImage(screenshot)

        # Update the label with the new image
        label.config(image=img_tk)
        label.image = img_tk

        # Call this function again after 1 second (1000 milliseconds)
        root.after(1000, update_image)
    else:
        print(f"No window found with the title '{window_title}'")


# Create the tkinter root window
root = tk.Tk()
root.title("Real-time Window Capture")

# Create a label to hold the image
label = tk.Label(root)
label.pack()

# Find the window with the specified title
windows = gw.getWindowsWithTitle(window_title)

# Start updating the image
update_image()

# Run the tkinter main loop
root.mainloop()

MSS实现

import cv2 as cv
import numpy as np
import pygetwindow as gw
import mss

# Define the window title
window_title = "Plants vs. Zombies"


def get_window_rect(title):
    windows = gw.getWindowsWithTitle(title)
    if not windows:
        raise ValueError(f"Window with title '{title}' not found.")
    return windows[0]._rect


def capture_screen(rect):
    with mss.mss() as sct:
        monitor = {"left": rect.left, "top": rect.top, "width": rect.width, "height": rect.height}
        img = sct.grab(monitor)
        img_np = np.array(img)
        img_np = cv.cvtColor(img_np, cv.COLOR_BGRA2BGR)  # Convert BGRA to BGR
        return img_np


def main():
    try:
        rect = get_window_rect(window_title)
        print("Monitoring Window Rect:", rect)

        while True:
            img = capture_screen(rect)
            cv.imshow("Plants vs. Zombies", img)

            # Exit loop on 'q' key press
            if cv.waitKey(1) == ord('q'):
                break

    except Exception as e:
        print(f"Error: {e}")

    cv.destroyAllWindows()


if __name__ == "__main__":
    main()

相关推荐

  1. Qt: windows下关闭系统

    2024-07-23 04:48:02       35 阅读
  2. D365 子调用父方法

    2024-07-23 04:48:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 04:48:02       75 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 04:48:02       80 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 04:48:02       64 阅读
  4. Python语言-面向对象

    2024-07-23 04:48:02       75 阅读

热门阅读

  1. 【开源库学习】libodb库学习(十)

    2024-07-23 04:48:02       18 阅读
  2. 【Python】探索 Python 中的 divmod 方法

    2024-07-23 04:48:02       16 阅读
  3. 开发面试算法题求教

    2024-07-23 04:48:02       16 阅读
  4. 容器化Mojo模型:轻量级部署的艺术

    2024-07-23 04:48:02       16 阅读
  5. Fiddler模拟弱网和移动网络环境

    2024-07-23 04:48:02       20 阅读
  6. 江苏省生产经营单位安全管理考核题库及答案

    2024-07-23 04:48:02       23 阅读
  7. 解决云服务器CPU占用率接近100%问题

    2024-07-23 04:48:02       22 阅读
  8. js进阶之作用域、函数进阶以及解构赋值

    2024-07-23 04:48:02       18 阅读
  9. 代码随想录第十六天|贪心算法(2)

    2024-07-23 04:48:02       23 阅读