有没有办法通过 Python 3 最小化 Windows 7 中的窗口? [英] Is there a way to minimize a window in Windows 7 via Python 3?

查看:20
本文介绍了有没有办法通过 Python 3 最小化 Windows 7 中的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个带有内置 Python 解释器的程序.定期我希望该程序能够全屏"或最小化.

I am running a program with a built-in Python interpreter. Periodically I want that program to be able to go "full screen" or be minimized.

这将在 Windows 7 上运行.

This will be running on Windows 7.

我想知道是否有办法在 Python 中执行此操作(以便我可以从我的程序中调用该函数).仅使用标准库是理想的,但如果有办法使用外部模块来做到这一点,那也很好.

I am wondering if there is a way to do this in Python (so that I could call the function from my program). Using only standard libraries would be ideal but if there is a way to do this with an external module, that would be fine too.

谢谢.

推荐答案

要最小化窗口,您需要知道窗口的标题或其窗口类.当确切的窗口标题未知时,窗口类很有用.例如,以下脚本显示了两种不同的方法来最小化 Microsoft Windows 记事本应用程序,假设:

To minimize a window you need to know either the title of the window, or its window class. The window class is useful when the exact window title is not known. For example the following script shows two different ways to minimize the Microsoft Windows Notepad application assuming:

import ctypes

notepad_handle = ctypes.windll.user32.FindWindowW(None, "Untitled - Notepad")
ctypes.windll.user32.ShowWindow(notepad_handle, 6)

notepad_handle = ctypes.windll.user32.FindWindowW(u"Notepad", None) 
ctypes.windll.user32.ShowWindow(notepad_handle, 6)  

要确定要使用的类名,您需要使用诸如 Microsoft 的 Spy++ 之类的工具.显然,如果用文件打开记事本,它会有一个不同的标题,例如 test.txt - Notepad.如果是这种情况,第一个示例现在将无法找到窗口,但第二个示例仍然可以工作.

To determine the class name to use, you would need to use an tool such as Microsoft's Spy++. Obviously if Notepad was opened with a file, it would have a different title such as test.txt - Notepad. If this was the case, the first example would now fail to find the window, but the second example would still work.

如果有两份记事本正在运行,那么只有一份会被关闭.如果需要关闭所有副本,则需要枚举所有需要更多代码的窗口.

If two copies of notepad were running, then only one would be closed. If all copies needed to be closed, you would need to enumerate all windows which requires more code.

ShowWindow 命令也可用于恢复窗口.这里使用的 6SW_MINIMIZE 的 Windows 代码.

The ShowWindow command can also be used to restore the Window. The 6 used here is the Windows code for SW_MINIMIZE.

这篇关于有没有办法通过 Python 3 最小化 Windows 7 中的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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