【问题标题】:How to end Tkinter propbably if not using the Quit button如果不使用退出按钮,如何可能结束 Tkinter
【发布时间】:2014-12-20 05:30:09
【问题描述】:

我试图找到解决问题的方法,但找不到。 我在 Windows 7 上使用 Python27。

我有一个带有按钮的简单 Tkinter GUI:

import Tkinter
import sys

def close_window():
    root.destroy()
    sys.exit()


root = Tkinter.Tk()

#exit button
draw_button = Tkinter.Button(root, text="Quit", command = close_window)
draw_button.grid(row=1, column=1)

root.mainloop()

现在,如果我使用“退出”按钮,程序将关闭,并且程序没有剩余任务。问题是如果有人使用 X 按钮关闭 Windows 或例如使用 Alt+F4,任务仍在运行。 为了以后使用,我冻结脚本以生成可执行文件,如果有人使用某种方法关闭程序,除了退出按钮,任务仍在运行。如果任务仍在运行,他或她将无法再次打开程序,因为它仍在后台运行,并且 Windows 会引发程序仍在运行的错误。

我尝试在主循环之后添加一些命令,但它们都被忽略了。我该如何解决这个问题?

感谢您的帮助!最大

【问题讨论】:

    标签: python tkinter terminate


    【解决方案1】:

    使用WM_DELETE_WINDOW 怎么样。例如:

    import tkinter
    import sys
    
    def close_window():
        root.destroy()
        sys.exit()
    
    def win_deleted():
        print("closed");
        close_window();
    
    root = tkinter.Tk()
    
    #exit button
    draw_button = tkinter.Button(root, text="Quit", command = close_window)
    draw_button.grid(row=1, column=1)
    
    root.protocol("WM_DELETE_WINDOW", win_deleted)
    
    root.mainloop()
    

    这将使用 ctr+F4 关闭应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多