本快速指南演示瞭如何使用Python控制Windows。 利用Win32 API和有用的包裝庫,Python提供了對各種Windows功能的強大控制。
密鑰概念:
win32 API訪問:
模塊允許與Windows註冊表進行直接交互,而無需其他安裝。 這對於檢索系統信息很有用。
winreg
winreg
>
PyWin32
PyGetWindow
> Windows註冊表交互:
請注意,使用原始字符串()在Windows路徑中正確處理背斜線。
winreg
利用Windows API和包裝庫:
import winreg hive = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) key = winreg.OpenKey(hive, r"SOFTWARE\Microsoft\Windows\CurrentVersion") value, type = winreg.QueryValueEx(key, "ProgramFilesDir") print(value) # Output: C:\Program Files
模塊解鎖Win32 API的功率。 要使用r"..."
:
簡化了窗口管理:
PyWin32
SHGetKnownFolderPath
>提供
from win32com.shell import shell, shellcon print(shell.SHGetKnownFolderPath(shellcon.FOLDERID_ProgramFiles, 0)) # Output: C:\Program Files
,PyGetWindow
,
import pygetwindow as gw edge_windows = gw.getWindowsWithTitle("Microsoft Edge") if edge_windows: edge_windows[0].minimize() # Minimize the first Edge window found
,PyGetWindow
>。
getWindowsWithTitle()
getActiveWindow()
安裝和更多信息:resizeTo()
moveTo()
>
minimize()
maximize()
bringToFront()
> pygetWindow:
>。 > pywin32:
>使用pip install pygetwindow
,可在SitePoint Premium和各種電子書零售商處獲得。 請參閱本書以獲取更多詳細的解釋和高級技術。 pip install pywin32
>pip install pygetwindow
(如果需要)。
pip3
>getWindowsWithTitle()
>最小化/最大化:minimize()
maximize()
>移動/調整窗口:moveTo()
resizeTo()
>前面:bringToFront()
檢查可見性:isVisible
關閉Windows:pyautogui.hotkey('alt', 'f4')
>和以上是快速提示:用Python控制窗戶的詳細內容。更多資訊請關注PHP中文網其他相關文章!