穿线不成功
Threading not successful
我正在做 Tkinter。我的功能 运行 正确(每分钟播放一次铃声),但它不是 运行 在一个线程上。每当我单击“开始”按钮时,程序 window 变灰并且顶部显示 "Not responding"(因为我对 start() 的递归调用正在锁定它,我假设。)
为什么我穿线不正确?谢谢
def start():
now = tt1.time()
listOfTimes = []
for t in timeSlotEFs:
listOfTimes.append(t.get())
now = datetime.now()
timee = now.strftime('%H%M')
print('here')
for t in listOfTimes:
if t==timee:
winsound.PlaySound('newhourlychimebeg.wav',winsound.SND_FILENAME)
s.enterabs(now+60,1,start) #I want to call recursively every 60 seconds
s.run()
def start2():
t = threading.Thread(target=start)
t.run()
startBtn = Button(ssFrame, text='Start', command=start2)
startBtn.grid(row=0,column=0,padx=paddX,pady=paddY)
感觉就像您混淆了 threading.Thread 从中导入的定义。
您应该创建 运行 函数,然后 启动 线程。
这样我看到了结果并且有效:
from tkinter import *
import threading
root = Tk()
def run():
print('hello Thread')
def start2():
t = threading.Thread(target=run)
t.start()
print(t) # Output: <Thread(Thread-1, stopped 10580)>
startBtn = Button(root, text='Start', command=start2)
startBtn.grid(row=0,column=0)
root.mainloop()
在start2()
中,我把t.run()
改成了t.start()
。就是这样,它现在可以工作了。谢谢吉姆。
我正在做 Tkinter。我的功能 运行 正确(每分钟播放一次铃声),但它不是 运行 在一个线程上。每当我单击“开始”按钮时,程序 window 变灰并且顶部显示 "Not responding"(因为我对 start() 的递归调用正在锁定它,我假设。)
为什么我穿线不正确?谢谢
def start():
now = tt1.time()
listOfTimes = []
for t in timeSlotEFs:
listOfTimes.append(t.get())
now = datetime.now()
timee = now.strftime('%H%M')
print('here')
for t in listOfTimes:
if t==timee:
winsound.PlaySound('newhourlychimebeg.wav',winsound.SND_FILENAME)
s.enterabs(now+60,1,start) #I want to call recursively every 60 seconds
s.run()
def start2():
t = threading.Thread(target=start)
t.run()
startBtn = Button(ssFrame, text='Start', command=start2)
startBtn.grid(row=0,column=0,padx=paddX,pady=paddY)
感觉就像您混淆了 threading.Thread 从中导入的定义。 您应该创建 运行 函数,然后 启动 线程。
这样我看到了结果并且有效:
from tkinter import *
import threading
root = Tk()
def run():
print('hello Thread')
def start2():
t = threading.Thread(target=run)
t.start()
print(t) # Output: <Thread(Thread-1, stopped 10580)>
startBtn = Button(root, text='Start', command=start2)
startBtn.grid(row=0,column=0)
root.mainloop()
在start2()
中,我把t.run()
改成了t.start()
。就是这样,它现在可以工作了。谢谢吉姆。