实现一个时钟,无论主线程怎么卡住,都不影响时钟正常进行走时间,但是这个程序,当主进程卡住时,子线程也不运行了怎么回事,求解。
from tkinter import *
from tkinter import messagebox
from sql.db import DBSqlite
import threading
import datetime
import time
root = Tk()
########S 窗口配置########
root.wm_title('sy-xiao')
width, height = (400,400)
root.minsize(400,400)
root.maxsize(600,600)
winWidth = int((root.winfo_screenwidth()-width)/2)
winHeight = int((root.winfo_screenheight()-height)/3)
root.geometry(f"{width}x{height}+{winWidth}+{winHeight}")
########E 窗口配置########
########S 创建一个输入标签########
Label(root, text="当前时间:").grid(row=0, column=0)
Label(root, text="任务名称:").grid(row=1, column=0)
taskNameEntry = Entry(root)
taskNameEntry.grid(row=1, column=1)
class MyLabel:
def __init__(self, root):
self.root = root
self.value = StringVar()
self.value.set(datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S'))
self.label = Label(self.root,textvariable=self.value)
self.label.grid(row=0, column=1)
def run(self):
t = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
self.value.set(t)
self.label.after(1000,self.run)
time_label = MyLabel(root)
th = threading.Thread(target=time_label.run)
th.setDaemon(True)
th.start()
def choose():
value = StringVar()
Label(root,textvariable=value).grid()
for i in range(10):
time.sleep(0.5)
value.set(str(i))
messagebox.showinfo(title=None, message=str("测试"))
Button(root,text='点击',command=choose).grid(row=3,column=1)
root.mainloop()