让你的程序使用gui

2024-04-27 08:50:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我用AutoIt来做类似的事情,但有时我不得不作弊,因为语言没有那么强大,或者可能只是我很烂,我不能用它做那么多:p

所以。。。我需要:

  • 截图,然后我会比较它们,使程序“明白”,但它需要“看”
  • 使用鼠标:移动,点击和释放,很简单,不是吗?在
  • 使用键盘:按一些键或组合键,包括特殊键,如AltCtrl等。。。在

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


Tags: 程序语言内容屏幕linuxwindows鼠标键盘
3条回答

我在使用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.

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

AutoIt完全可以做你提到的一切。当我想做一些自动化但是使用Python的特性时,我发现使用AutoItX是一个DLL/COM控件最简单。在

取自我的this answer

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

您可以在Windows下使用WATSUP。在

相关问题 更多 >