概要
tkinterでthreadやってみた。
threading.Eventで、thread止めてみた。
サンプルコード
from tkinter import *
import time
import threading
def _delete_window():
try:
flg.clear()
time.sleep(2)
root.destroy()
except:
pass
def draw():
while flg.is_set():
str.set(time.strftime('%I:%M:%S'))
time.sleep(1)
def func0():
flg.set()
t = threading.Thread(target = draw)
t.start()
print ('start')
def func1():
flg.clear()
print ('stop')
flg = threading.Event()
flg.set()
root = Tk()
root.protocol("WM_DELETE_WINDOW", _delete_window)
frame = Frame(root)
frame.grid(row = 3, column = 3)
startbutton = Button(frame, text = "Start", command = func0)
startbutton.grid(row = 0, column = 0, sticky = NSEW)
stopButton = Button(frame, text = "Stop", command = func1)
stopButton.grid(row = 0, column = 1, sticky = NSEW)
str = StringVar()
str.set('')
label = Label(frame, textvariable = str)
label.grid(row = 0, column = 2, sticky = NSEW)
t = threading.Thread(target = draw)
t.start()
root.mainloop()
以上。