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

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

How to close a specific application on windows using python?

问题

  1. 我正在自学python所以经验非常有限希望你们能帮忙
  2. 具体的问题是
  3. 我正在开发一个语音通信虚拟助手我已经成功通过语音打开了应用程序但无法编写关闭特定名称的运行中应用程序的代码
  4. 我尝试了使用命令
  5. `send_keys('%{F4}')`
  6. 似乎没有得到期望的结果
  7. 那么还有其他办法吗
  8. elif "discord" in you:
  9. app = application.Application()
  10. app.start("C://Users//admin//AppData//Local//Discord//Update.exe --processStart Discord.exe")
  11. robot_brain = "运行中的Discord"
  12. elif you == "退出discord":
  13. send_keys('%{F4}') # 使用Alt+F4关闭活动窗口
  14. 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?

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

答案1

得分: 1

你也可以使用 taskkill.

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

You can also use taskkill.

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

答案2

得分: 0

  1. 你可以使用 os 模块来启动文件和启动一个 exe 文件,以及退出它。
  2. 示例:
  3. import os
  4. os.system("C:/Users/username/Downloads/main.exe")
  5. 这会启动文件(如果你想打开像 txt 这样的文件而不是 exe 文件,可以使用 startfile 替代 system)。
  6. 有许多方法来关闭它,以下是一些有用的链接:
  7. https://stackoverflow.com/questions/5625524/how-to-close-a-program-using-python
  8. 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

  1. import os
  2. 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:

确定