简体   繁体   English

不关闭Tkinter窗口退出python程序

[英]exit python program without close Tkinter window

when I was working with Tkinter in python, I encountered an interesting problem that when I run a python program in Linux Terminal, my code opened a Tkinter widget like this: 当我在python中使用Tkinter时,遇到一个有趣的问题,当我在Linux Terminal中运行python程序时,我的代码打开了一个Tkinter小部件,如下所示:

root = Tk()
root.title("Hello World")
txt = Text(root, width=60, height=20, wrap="word")
txt.insert(INSERT, "HELLO WORLD")
txt.insert(END, "---------------END---------------")
txt.config(state=DISABLED)
txt.bind("<Leave>", close_when_lost_focus)
txt.pack()
root.mainloop()

and then I force quit python program with Ctrl + Z , the Tkinter window was still there and I couldn't close it until I closed the Terminal. 然后我用Ctrl + Z强制退出python程序,Tkinter窗口仍然存在,直到关闭终端后我才能关闭它。 Any body knows what happened? 任何人都知道发生了什么事吗?

control-z doesn't force quit a program. control-z不会强制退出程序。 It is more like putting it to sleep, or suspending it. 它更像是使其休眠或暂停。 You can cause the program to resume by typing the command fg (foreground) or bg (background). 您可以通过键入命令fg (前景)或bg (背景)使程序恢复。

fg brings it to the foreground, which makes it as if you never typed control-z. fg将其带到前台,就好像您从未键入control-z一样。

bg places the job in the background and lets it continue running while allowing you to type additional commands in the terminal. bg将作业放在后台,并使其继续运行,同时允许您在终端中键入其他命令。

For more information see What is effect of CTRL + Z on a unix\\Linux application 有关更多信息,请参见在Unix \\ Linux应用程序上CTRL + Z的作用是什么

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

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