简体   繁体   English

Python 关闭 windows 中所有打开的应用程序的代码

[英]Python code to close all open application in windows

I am working on pyautogui for locateonscreen but I need to make sure I have a clean windows prior to running it.我正在为 locateonscreen 使用 pyautogui,但我需要确保在运行它之前我有一个干净的 windows。

I need help with a code that will kill/close all open/active windows application and then open a single exe using subprocess.我需要有关代码的帮助,该代码将终止/关闭所有打开/活动的 windows 应用程序,然后使用子进程打开单个 exe。

you can use the psutil library to close all open/active windows applications and then open a single exe using the subprocess module:您可以使用 psutil 库关闭所有打开/活动的 windows 应用程序,然后使用子进程模块打开单个 exe:

import psutil
import subprocess

# Kill all open/active processes
for proc in psutil.process_iter():
    proc.kill()

# Open a single exe using subprocess
subprocess.Popen("C:\\path\\to\\exe.exe")

Note that the psutil.process_iter() returns a list of all running processes on the system, and proc.kill() method is used to kill each process.请注意,psutil.process_iter() 返回系统上所有正在运行的进程的列表,proc.kill() 方法用于杀死每个进程。 The subprocess.Popen method is used to open the exe file. subprocess.Popen方法用于打开exe文件。 Make sure that you provide the correct path to the exe file.确保提供 exe 文件的正确路径。 Please be aware that this script will close all open applications, including any unsaved work in progress.请注意,此脚本将关闭所有打开的应用程序,包括任何未保存的正在进行的工作。 You should use it with caution, or for testing purpose only.您应该谨慎使用它,或仅用于测试目的。

you can instead reduce all open windows. this is safer!您可以改为减少所有打开的 windows。这样更安全!

import win32gui
import win32con
import subprocess

def minimize_all():
    def callback(hwnd, hwnds):
        if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
            hwnds.append(hwnd)
        return True

    hwnds = []
    win32gui.EnumWindows(callback, hwnds)
    for h in hwnds:
        win32gui.ShowWindow(h, win32con.SW_MINIMIZE)

minimize_all()
# Open a single exe using subprocess
subprocess.Popen("C:\\path\\to\\exe.exe")

This script uses the win32gui library to enumerate all open windows, and the win32con library to minimize them.此脚本使用 win32gui 库枚举所有打开的 windows,并使用 win32con 库将它们最小化。

why not minimize all windows with shortcut WIN + D ?为什么不使用快捷方式WIN + D最小化所有 windows ?

import pyautogui
pyautogui.hotkey('winleft','d')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM