让你的程序使用 GUI

发布于 2024-07-26 02:26:18 字数 450 浏览 15 评论 0 原文

我想编写一个程序,能够通过控制鼠标/键盘并能够“查看”屏幕上的内容来“使用”其他程序。

我使用 AutoIt 来做类似的事情,但有时我不得不作弊,因为该语言不是那么强大,或者也许只是我很糟糕,我无法用它做那么多:P

所以...我需要:

  • 截取屏幕截图,然后我将比较它们以使程序“理解”,但它需要“看”
  • 使用鼠标:移动、单击、释放,很简单,不是吗?
  • 使用键盘:按一些键或组合键,包括特殊键,如 AltCtrl 等...

我如何在 python 中做到这一点?
它在linux和windows下都可以工作吗? (这可能真的很酷,但没有必要)

I'd like to write a program able to "use" other programs by taking control of the mouse/keyboard and being able to "see" what's on the screen.

I used AutoIt to do something similar, but I had to cheat sometimes because the language is not that powerful, or maybe it's just that I suck and I'm not able to do that much with it :P

So... I need to:

  • Take screenshots, then I will compare them to make the program "understand", but it needs to "see"
  • Use the mouse: move, click and release, it's simple, isn't it?
  • Using the keyboard: pressing some keys, or key combinations, including special keys like Alt,Ctrl etc...

How can I do that in python?
Does it works in both linux and windows? (this could be really really cool, but it is not necessary)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

守护在此方 2024-08-02 02:26:18

我使用 PyWinAuto 成功完成了类似的任务。

pywinauto是一组python模块
自动化 Microsoft Windows GUI。
最简单的是,它允许您发送
对窗口的鼠标和键盘操作
对话框和控件。

它还支持使用 Python 图像库 PIL 捕获对话框图像。

I've had some luck with similar tasks using PyWinAuto.

pywinauto is a set of python modules
to automate the Microsoft Windows GUI.
At it's simplest it allows you to send
mouse and keyboard actions to windows
dialogs and controls.

It also has some support for capturing images of dialogs and such using the Python Imaging Library PIL.

冷情妓 2024-08-02 02:26:18

AutoIt 完全有能力完成你提到的所有事情。 当我想做一些自动化但使用 Python 的功能时,我发现使用 AutoItX 其中是一个 DLL/COM 控件。

取自我的这个答案

import win32com.client
oAutoItX = win32com.client.Dispatch( "AutoItX3.Control" )

oAutoItX.Opt("WinTitleMatchMode", 2) #Match text anywhere in a window title

width = oAutoItX.WinGetClientSizeWidth("Firefox")
height = oAutoItX.WinGetClientSizeHeight("Firefox")

print width, height

AutoIt is completely capable of doing everything you mentioned. When I'm wanting to do some automation but use the features of Python, I find it easiest to use AutoItX which is a DLL/COM control.

Taken from this answer of mine:

import win32com.client
oAutoItX = win32com.client.Dispatch( "AutoItX3.Control" )

oAutoItX.Opt("WinTitleMatchMode", 2) #Match text anywhere in a window title

width = oAutoItX.WinGetClientSizeWidth("Firefox")
height = oAutoItX.WinGetClientSizeHeight("Firefox")

print width, height
佼人 2024-08-02 02:26:18

您可以在 Windows 下使用 WATSUP

You can use WATSUP under Windows.

掩饰不了的爱 2024-08-02 02:26:18

如果您熟悉 pascal,那么 SCAR 是一个真正强大的键盘/鼠标/屏幕阅读程序: http://freddy1990.com/index.php?page=product&name=scar 它可以进行OCR、位图查找、颜色查找等。它经常用于自动化在线游戏,但它可以适用于您想要模拟人类阅读屏幕并提供输入的任何情况。

If you are comfortable with pascal, a really powerful keyboard/mouse/screen-reading program is SCAR: http://freddy1990.com/index.php?page=product&name=scar It can do OCR, bitmap finding, color finding, etc. It's often used for automating online games, but it can be used for any situation where you want to simulate a human reading the screen and giving input.

念﹏祤嫣 2024-08-02 02:26:18

I've used the Windows (only) Input API to write a VNC-like remote-control application in the past. It lets you fake keyboard and mouse input nicely at a system level (ie not just posting events to a single application).

If you're trying to do any sort of automated testing of whole systems at the GUI level, this excellent USENIX paper describing automated responsiveness testing is a must-read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文