0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tkinterでthread その3

Posted at

概要

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()


以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?