如何使用Python关闭Windows上的特定应用程序?

huangapple go评论62阅读模式
英文:

How to close a specific application on windows using python?

问题

我正在自学python所以经验非常有限希望你们能帮忙
具体的问题是
我正在开发一个语音通信虚拟助手我已经成功通过语音打开了应用程序但无法编写关闭特定名称的运行中应用程序的代码
我尝试了使用命令
`send_keys('%{F4}')`
似乎没有得到期望的结果
那么还有其他办法吗

        elif "discord" in you:
		app =  application.Application()
		app.start("C://Users//admin//AppData//Local//Discord//Update.exe --processStart Discord.exe")
		robot_brain = "运行中的Discord"
	elif you == "退出discord":
		send_keys('%{F4}') # 使用Alt+F4关闭活动窗口
		robot_brain = "退出discord"
英文:

I'm self-studying python so I'm very inexperienced so I hope you guys can help
The specific problem is:
I'm building a voice communication virtual assistant, I have managed to open the application by voice, but can't write the code to close a running application with a specific name.
I tried using the command
send_keys('%{F4}')
and doesn't seem to get the desired result
So is there any other way?

    elif "discord" in you:
	app =  application.Application()
	app.start("C://Users//admin//AppData//Local//Discord//Update.exe --processStart Discord.exe")
	robot_brain = "runing Discord"
elif you == " exit discord":
	send_keys('%{F4}') # close an active window with Alt+F4
	robot_brain = " exit discord"

答案1

得分: 1

你也可以使用 taskkill.

proc = subprocess.Popen(['taskkill', '/f', '/im', 'program_name.exe'])
assert proc.wait() == 0
英文:

You can also use taskkill.

proc = subprocess.Popen(['taskkill', '/f', '/im', 'program_name.exe'])
assert proc.wait() == 0

答案2

得分: 0

你可以使用 os 模块来启动文件和启动一个 exe 文件,以及退出它。
示例:

import os
os.system("C:/Users/username/Downloads/main.exe")

这会启动文件(如果你想打开像 txt 这样的文件而不是 exe 文件,可以使用 startfile 替代 system)。

有许多方法来关闭它,以下是一些有用的链接:

https://stackoverflow.com/questions/5625524/how-to-close-a-program-using-python
https://docs.python.org/3/library/os.html#os.kill
英文:

Hello, im not too sure what the app is (in your code) but you can use the os module to start files and to start a exe and to exit it
example

import os
os.system("C:/Users/username/Downloads/main.exe")

this starts the file (use startfile instead of system if you want to open like a txt or anything not exe file)

their is alot of way to close it heres some usefull links

https://stackoverflow.com/questions/5625524/how-to-close-a-program-using-python
https://docs.python.org/3/library/os.html#os.kill

huangapple
  • 本文由 发表于 2023年2月19日 23:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定