Python 使用线程时如何在后台关闭正在运行的CMD?

Python 使用线程时如何在后台关闭正在运行的CMD?,python,tkinter,Python,Tkinter,代码 import tkinter as tk from tkinter import * import threading def main(): root = tk.Tk() # Gets the requested values of the height and width. window_width = root.winfo_reqwidth() window_height = root.winfo_reqheight() # Gets bot

代码

import tkinter as tk
from tkinter import *
import threading

def main():
    root = tk.Tk()
    # Gets the requested values of the height and width.
    window_width = root.winfo_reqwidth()
    window_height = root.winfo_reqheight()
    # Gets both half the screen width/height and window width/height
    position_right = int(root.winfo_screenwidth()/2 - window_width/2)
    position_down = int(root.winfo_screenheight()/2 - window_height/2)
    # Positions the window in the center of the page.
    root.geometry(f"+{position_right}+{position_down}")
    app = MainWindow(root)
    root.mainloop()

class MainWindow:
    def __init__(self,master):
        self.master = master
        self.master.title("Sign off check")
        self.master.geometry('800x350')
        self.frame = Frame(self.master)
        self.frame.pack(fill="both", expand=True)

        def refresh_progress():
            threading.Timer(5.0, refresh_progress).start()

        refresh_progress()

if __name__ == '__main__':
    main()
输出

通过CMD运行-打开简单的空白Tkinter窗口,每5秒运行一次函数(
def refresh\u progress():

问题

我的问题是-只要我关闭Tkinter窗口(按“x”右上角)。CMD窗口仍在后台运行

问题

Tkinter窗口关闭后,如何结束CMD在后台运行?

在这里使用我的完整代码

import sys
import tkinter as tk
from tkinter import *
import threading

def on_closing():
    # you can add a various command here, for example like messagebox or something
    timer.cancel()
    root.destroy()
    sys.exit()

def main():
    global root
    root = tk.Tk()
    root.protocol("WM_DELETE_WINDOW", on_closing) # when user click "X"
    # Gets the requested values of the height and width.
    window_width = root.winfo_reqwidth()
    window_height = root.winfo_reqheight()
    # Gets both half the screen width/height and window width/height
    position_right = int(root.winfo_screenwidth()/2 - window_width/2)
    position_down = int(root.winfo_screenheight()/2 - window_height/2)
    # Positions the window in the center of the page.
    root.geometry(f"+{position_right}+{position_down}")
    app = MainWindow(root)
    root.mainloop()

class MainWindow:
    def __init__(self,master):
        self.master = master
        self.master.title("Sign off check")
        self.master.geometry('800x350')
        self.frame = Frame(self.master)
        self.frame.pack(fill="both", expand=True)

        def refresh_progress():
            global timer
            timer = threading.Timer(5.0, refresh_progress)
            timer.start()

        refresh_progress()

if __name__ == '__main__':
    main()


通过将协议“WM_DELETE_WINDOW”添加到
root

解决了此问题。您好,谢谢您的回答!这会在我每次尝试时冻结应用程序。哦,是的,我忘了添加根目录。让我们再试一次根目录未定义重试好的,我修复了
root