Python如何設(shè)置指定窗口為前臺(tái)活動(dòng)窗口
Python程序運(yùn)行時(shí),打開了多個(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)。
相關(guān)文章:
1. html中的form不提交(排除)某些input 原創(chuàng)2. ASP常用日期格式化函數(shù) FormatDate()3. 開發(fā)效率翻倍的Web API使用技巧4. XMLHTTP資料5. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效6. asp.net core項(xiàng)目授權(quán)流程詳解7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. CSS3中Transition屬性詳解以及示例分享9. jsp文件下載功能實(shí)現(xiàn)代碼10. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享
