简体   繁体   English

在 IDE 中,如何正确关闭 Python 程序?

[英]How can I close a Python program properly when in an IDE?

From what I understand, using sys.exit is not compatible with PyCharm and other IDEs, yet other exit methods that don't do garbage collection are not recommended.据我了解,使用 sys.exit 与 PyCharm 和其他 IDE 不兼容,但不建议使用其他不进行垃圾回收的退出方法。 If I'm developing in PyCharm how can I properly shut down the program?如果我在 PyCharm 中开发,我该如何正确关闭程序?

In the below example I am attempting to use a hot key to exit the program, yet the test statement is never reached.在下面的示例中,我尝试使用热键退出程序,但从未到达测试语句。 Is this an incorrect syntax or related to using the wrong exit command?这是不正确的语法还是与使用错误的退出命令有关?

#Import modules
from tkinter import * #For images
from PIL import Image, ImageTk #For images
import os #For working directory
from pynput import keyboard #For hotkeys
#Set default directory
os.chdir('C:\\Users\\UserName\\Desktop\\Python\\SomeFolderName')

#Display Menu
root = Tk()
image = Image.open('menu.png')
display = ImageTk.PhotoImage(image)
root.overrideredirect(True) #Remove toolbar
label = Label(root, image=display)
label.pack()
root.mainloop()

#Functions
def ExitProgram():
print('test')
sys.exit(0)

#Define hotkeys
with keyboard.GlobalHotKeys({
'<ctrl>+w': ExitProgram,
'<ctrl>+0': ExitProgram}) as h:
h.join()

Use the quit() function to exit the program.使用quit() function 退出程序。 This is a built-in function from Python in order to exit the program.这是来自 Python 的内置 function 以退出程序。

Happy Coding!快乐编码!

This answer from a similar question might be a useful answer for you. This answer from a similar question可能对您有用。

Python exit commands - why so many and when should each be used? Python 退出命令 - 为什么要使用这么多以及何时使用?

I actually use VS code and sys.exit(0) is fine, but can also use quit() which is also fine.我实际上使用 VS 代码和sys.exit(0)很好,但也可以使用quit()也很好。

Use quit() or exit() to terminate your program.使用quit()exit()来终止您的程序。 quit() and exit() are synonymous and do the exact same thing. quit()exit()是同义词,做的事情完全相同。

In addition to calling sys.exit() , using quit() does some other internal cleanup, such as closing input streams.除了调用sys.exit()之外,使用quit()还可以进行其他一些内部清理,例如关闭输入流。

They are briefly discussed in the official documentation here: https://docs.python.org/3/library/constants.html#quit它们在这里的官方文档中进行了简要讨论: https://docs.python.org/3/library/constants.html#quit

Nothing past root.mainloop() will execute because it never returns. root.mainloop()任何内容都不会执行,因为它永远不会返回。 Move the with keyboard ... code before root.mainloop() .root.mainloop()之前移动with keyboard ... 代码。

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

相关问题 如何正确关闭Python exe控制台程序 - How to properly close Python exe console program 手动关闭程序时可以让python做些什么吗? - Can I let python do something when manually close the program? 硒中的程序被意外杀死时,如何关闭浏览器? - How can I close the browser when program is killed accidentally in selenium? 使用cmd python模块时,如何使我的程序正确崩溃? - How can I make my program properly crash when using the cmd python module? 如何使用运行模块功能在Komodo IDE中像在IDLE(Python GUI)中一样运行python程序? - How can I run a python program in Komodo IDE like in IDLE(Python GUI) using Run Module feature? Python - Stray cmd.exe窗口导致程序挂起。 我如何关闭这个迷路程序? - Python - Stray cmd.exe window causes program to hang. How can I close this stray program? 我如何关闭由python程序启动的子进程而不关闭程序 - how can i close a subprocess started by python program without closing the program 如何删除CMD指令,以便可以在IDE控制台(Python)中运行程序? - How to delete CMD instructions so I can run program in IDE console (Python)? 如何在 Pythonista IDE 中正确使用 python turtle() 函数? - How do I properly use the python turtle() function in the Pythonista IDE? 自动点击远离 IDE 时如何停止 python 程序? - How to stop a python program when auto clicking away from the IDE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM