当 tkinter 根窗口关闭时,如何杀死已经运行的线程。我能够检测事件,但如果用户尚未启动任何线程,我在关闭窗口时会遇到错误。另外,当关闭时我是否能够检测到线程正在使用函数运行
self.thread.is_alive()
使用什么命令来杀死线程?
import threading
import tkinter as tk
from tkinter import messagebox
import time
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.master.title("myapp")
master.protocol("WM_DELETE_WINDOW", self.on_closing)
def createWidgets(self):
self.btn = tk.Button(self)
self.btn["text"] = "Start.."
self.btn.pack()
self.btn["command"] = self.startProcess
def startProcess(self):
self.thread = threading.Thread(target=self.helloWorld, args=("Hello World",))
self.thread.start()
def helloWorld(self, txt):
for x in range(5):
print (txt)
time.sleep(5)
def on_closing(self):
if messagebox.askokcancel("myapp", "Do you want to quit?"):
if self.thread.is_alive():
self.thread.stop()
self.master.destroy()
def main():
root = tk.Tk()
app = Application(master=root) …Run Code Online (Sandbox Code Playgroud) 我需要删除某些 txt 文件中以“#”开头的行。但忽略第一行作为标题。如何使 grep 忽略第一行并删除其余行中以 # 开头的任何行?
cat sample.txt
#"EVENT",VERSION, NAME
1,2,xyz
1,2,abc
1,2,asd
1,2,ert
#"EVENT",VERSION, NAME
1,2,xyz
1,2,abc
1,2,xyz
cat sample.txt | grep -v "^\s*[#\;]\|^\s*$" > "out.txt"
Run Code Online (Sandbox Code Playgroud)
但这也删除了标题!