用 Python 切换 Windows [英] Switching Windows with Python

查看:35
本文介绍了用 Python 切换 Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会先说我对 Python 非常缺乏经验,我希望这意味着我的问题的解决方案会很简单.

I'll preface this by saying that I'm very inexperienced with python, and I'm hoping that means the solution to my problem will be simple.

我的程序将在另一个窗口中执行简单的操作,因此理想情况下,我希望我的脚本在执行其余命令之前确保另一个窗口最大化并处于活动状态.事实证明,这比我预期的要困难得多.

My program will be performing simple actions in another window, so ideally I would like my script to make sure that this other window is maximized and active before it proceeds with the rest of the commands. This has proved to be much more difficult than I had expected.

我相当确定我可以通过使用 find_window 并将其设置为前台的 win32gui 来实现这一点.当我遇到上一个问题时,我以为我已经找到了解决方案:Python 窗口激活

I am fairly certain that I can achieve this with win32gui using find_window and setting it to the foreground. I thought I had found my solution when I came across this previous question: Python Window Activation

很遗憾,由于以下几个原因,我无法使用解决方案代码或对其进行操作来解决我的问题:

Unfortunately I was unable to use the solution code or manipulate it to solve my problem for a few reasons:

- 用户定义 find_window 的方式只允许您通过窗口的类名进行选择,我不知道也找不到,因为它只是一个运行在 Java 中的游戏.我似乎无法更改该行以适用于特定的窗口名称(我知道),因为它不是默认参数".

-The way that user had defined find_window only allows you to choose by the classname of the window, which I don't know and haven't been able to find since it is just a game running in Java. I couldn't seem to change that line to work for the specific windowname (Which I do know) as it is not the "default argument".

-我不想枚举窗口来找到它,因为我不确定它是如何工作的.

-I don't want to enumerate the windows to find it as I'm not sure how that would work.

- 使用脚本中编写的 find_window_wildcard 已设法将窗口置于前台几次,但前提是窗口已经打开,并且只能间歇性地工作.

-using find_window_wildcard as it's written in that script has managed to bring the window to the foreground a few times, but only if the window was already open, and it only works intermittently.

-set_foreground() 需要一个输入语句,无论我如何尝试设置它,我总是会收到错误消息,即我的元素数量错误或窗口上的句柄无效.

-set_foreground() requires one input statement and no matter how I try to set it I am always given an error that I either have the wrong number of elements or an invalid handle on the window.

我知道我很笨;但是对于这个问题的清晰简洁的解决方案,或者至少对 find_window/getwindow 语法的一个很好的解释,对我自己和其他遇到这个问题的人来说都是天赐之物.

I know that I'm stupid; but a clear and concise solution to this problem or at least a good explanation of the find_window/getwindow syntax would be a godsend to myself and anyone else who's having this problem.

推荐答案

强烈建议您查看 Swapypywinauto.在 UI 自动化方面,它们可以帮助您做一些很棒的事情.

I would strongly suggest you take a look at the pages for Swapy and for pywinauto. They can help you do some awesome things when it comes to UI Automation.

这篇关于用 Python 切换 Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆