欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python之tkinter多线程计时器

程序员文章站 2022-11-30 15:01:43
Python练习题(五)tkinter多线程计时器未完待续,,,tkinter多线程计时器import tkinter as tkfrom threading import Thread, Eventhour = 0class ControlThread(Thread): def __init__(self): self._stop_event = Event() Thread.__init__(self) def run(self):...


Python之tkinter多线程计时器

tkinter多线程计时器

import tkinter as tk
from threading import Thread, Event

hour = 0


class ControlThread(Thread):
    def __init__(self):
        self._stop_event = Event()
        Thread.__init__(self)

    def run(self):
        f1()

    def terminate(self):
        self._stop_event.set()

    def stopped(self):
        return self._stop_event.is_set()


def f1():  # 计时
    def count():
        global hour
        global x
        hour += 1
        hour %= 24
        label_hour.config(text=str(hour))
        x = label_hour.after(3600, count)
    count()


def run():
    global t1
    t1 = ControlThread()
    t1.start()


def stop():
    global t1
    label_hour.after_cancel(x)
    t1.terminate()


root = tk.Tk()

root.title("time")

label_hour = tk.Label(root, bg="lightblue", height=2, width=10)
label_hour.grid(row=0, column=0, columnspan=3)

button_run = tk.Button(root, text="Run", command=run)
button_run.grid(row=1, column=0)

button_stop = tk.Button(root, text="Stop", command=stop)
button_stop.grid(row=1, column=2)

root.mainloop()

未完待续,,,

本文地址:https://blog.csdn.net/hongwangdb/article/details/107139329