1 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
要循環(huán)選擇窗口,需要執(zhí)行以下幾個(gè)步驟:
使用win32gui.EnumWindows遍歷所有打開的窗口
使用win32gui.GetWindowText從窗口獲取標(biāo)題欄文本
使用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
添加回答
舉報(bào)