[英]How to close all opened windows from python?
我正在 python 中制作個人語音助手。 在我的程序中,我寫了一個 function 可以關閉我的電腦。 但我也想在關閉之前關閉所有打開的 window。 那么是否有任何 python 腳本或任何 windows 快捷鍵?
您可以使用子進程執行powershell
或DOS shutdown
腳本:
import subprocess
# forced shutdown closes windows without saving; you might need to add shell=True
COMMAND = "Stop-Computer -Force"
subprocess.run(['powershell', '-command', COMMAND], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
我們也可以關閉 windows 然后關機
# replace command above with below
COMMAND = """(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}; Get-Process | Where-Object {$_.MainWindowTitle -ne \"\"} | stop-process; Stop-Computer"""
DOS
# from shutdown documentation
# /s Shuts down the computer
# /f Forces running applications to close without warning users.
# /t <xxx> Sets the time-out period before shutdown to xxx seconds.
subprocess.run(['shutdown', '/s', '/t 0', '/f'], shell=False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.