成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python如何設(shè)置指定窗口為前臺(tái)活動(dòng)窗口

瀏覽:2日期:2022-07-14 13:17:48

Python程序運(yùn)行時(shí),打開(kāi)了多個(gè)窗口,使用win32gui模塊可以設(shè)置指定的某一個(gè)窗口為當(dāng)前活動(dòng)窗口。

import re, timeimport webbrowserimport win32gui, win32con, win32com.client def _window_enum_callback(hwnd, wildcard): ’’’ Pass to win32gui.EnumWindows() to check all the opened windows 把想要置頂?shù)拇翱诜诺阶钋懊?,并最大? ’’’ if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None: win32gui.BringWindowToTop(hwnd) # 先發(fā)送一個(gè)alt事件,否則會(huì)報(bào)錯(cuò)導(dǎo)致后面的設(shè)置無(wú)效:pywintypes.error: (0, ’SetForegroundWindow’, ’No error message is available’) shell = win32com.client.Dispatch('WScript.Shell') shell.SendKeys(’%’) # 設(shè)置為當(dāng)前活動(dòng)窗口 win32gui.SetForegroundWindow(hwnd) # 最大化窗口 win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE) if __name__ == ’__main__’: webbrowser.open('https://www.baidu.com/') time.sleep(1) win32gui.EnumWindows(_window_enum_callback, '.*%s.*' % config.window_name)#此處為你要設(shè)置的活動(dòng)窗口名

說(shuō)明一點(diǎn):

有人會(huì)遇到這個(gè)錯(cuò)誤(好吧,我也遇到了):

pywintypes.error: (0, ’SetForegroundWindow’, ’No error message is available’)

Stack Overflow上的解決方法是添加如下代碼:

shell = win32com.client.Dispatch('WScript.Shell')shell.SendKeys(’%’)

即先發(fā)送一個(gè)alt key事件,這個(gè)錯(cuò)誤就會(huì)避免,后面的設(shè)置才會(huì)有效。

鏈接地址:

https://stackoverflow.com/questions/14295337/win32gui-setactivewindow-error-the-specified-procedure-could-not-be-found

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章: