import pythoncom
........
# 设置窗口最前端代码部分
hld = win32gui.FindWindow(None, u"xxxxxxx标题")
if hld <= 0:
hld = win32gui.FindWindow(None, u"xxxxx标题")
if hld > 0:
# 设置为最前端窗口
win32gui.SetForegroundWindow(hld)
time.sleep(1)
以上代码出现error(0, 'SetForegroundWindow', 'No error message is available')错误,
解决方法:加上,
pythoncom.CoInitialize() shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%')
import pythoncom
........
# 设置窗口最前端代码部分
hld = win32gui.FindWindow(None, u"xxxxxxx标题")
if hld <= 0:
hld = win32gui.FindWindow(None, u"xxxxx标题")
if hld > 0:
pythoncom.CoInitialize()
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
# 设置为最前端窗口
win32gui.SetForegroundWindow(hld)
time.sleep(1)