1 回答
TA贡献1862条经验 获得超6个赞
要循环选择窗口,需要执行以下几个步骤:
使用win32gui.EnumWindows遍历所有打开的窗口
使用win32gui.GetWindowText从窗口获取标题栏文本
使用win32com.client.Dispatch和SendKeys激活切换过程
使用win32gui.SetForegroundWindow选择要激活的窗口
这是代码:
import win32com.client as win32
import win32gui
import time
title = "Untitled - Notepad2" # cycle all windows with this title
def windowEnumerationHandler(hwnd, top_windows):
top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
top_windows = [] # all open windows
win32gui.EnumWindows(windowEnumerationHandler, top_windows)
winlst = [] # windows to cycle through
for i in top_windows: # all open windows
if i[1] == title:
winlst.append(i)
for x in range(5): # cycle 5 times
for w in winlst: # each window with selected title
shell = win32.Dispatch("WScript.Shell") # set focus on desktop
shell.SendKeys('%') # Alt key
win32gui.SetForegroundWindow(w[0]) # bring to front, activate
time.sleep(2) # 2 seconds
添加回答
举报