如何最小化 Python 中的特定窗口 [英] How to minimize a specific window in Python

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

问题描述

您好,我是 Python 新手,不知道在这种情况下如何最小化特定窗口 Microsoft Word 2010 我只能最小化 Python Shell.这是我的代码,以防您需要它.

Hello I am new to Python and do not know how to minimize a specific window in this case Microsoft Word 2010 all I can minimize is the Python Shell. Here is my code just in case you need it.

import win32gui, win32con
import os
import math
import time

M=6
Minimize = win32gui.GetForegroundWindow()

print("Program Started on "+time.ctime())

while M >0:
time.sleep(1)
print(M," more seconds until Word is opened")
M -=1

time.sleep(1)    
os.startfile("C:\Documents and Settings\All Users\Start Menu\Programs\MicrosoftOffice\Microsoft Word 2010")
print("Microsoft Word 2010 opened "+time.ctime())

time.sleep(2)
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)

推荐答案

好吧 - 此代码中的错误是在您运行 win32gui.GetForegroundWindow() 时,没有 MS Word Window,并且当前的前台窗口可能是 Python shell.

Well - the error in this code is that at the time you run win32gui.GetForegroundWindow(), there is no MS Word Window, and the current foreground window is probably the Python shell.

试试:

time.sleep(2)
Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)

我认为如果您使用其他功能来选择正确的窗口会更好 - 而不是取决于超时.不确定,但 FindWindow 看起来像您需要的.

I think it would be better if you used some other function to select the right window - not depending on timeout. Not sure, but FindWindow looks like what you need.

这篇关于如何最小化 Python 中的特定窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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