如何在 tkinter 中为每个分屏添加计时器

How to add timer to each divide screen in tkinter

我创建了一个 window 并将其拆分为 3 个不同的 windows。我想为每个屏幕添加一个时钟,它不依赖于其他时钟。 我的代码打开 2 window- 一个是计时器,第二个是 window 我拆分为 3 windows.

    from tkinter import *
import Tkinter as tk


class split_screen(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.label = tk.Label(self, text="", width=10,fg="black", bg ="red", font="david 18 bold underline")
        self.label.pack()
        self.remaining = 0
        self.countdown(1000)
        self.configure(background="black")


    def screen(self):
        root = Tk()
        root.geometry("650x700")
        root.configure(bg='black')
        root.title("test")
        left = Frame(root, borderwidth=200, relief="solid")
        right = Frame(root, borderwidth=20, relief="solid")
        box3 = Frame(right, borderwidth=2, relief="solid")
        box1 = Frame(left, borderwidth=2, relief="solid")
        box2 = Frame(left, borderwidth=2, relief="solid")

        label1 = Label(box3, text="winner's" + "\n\n\n" + "Player 1",fg= "black", bg = "red", font = "david 18 bold underline")
        label2 = Label(box1, text="Computer 1",fg = "black", bg = "red", font= "david 18 bold underline")
        label3 = Label(box2, text="Computer 2",fg = "black", bg = "red", font= "david 18 bold underline")

        left.pack(side="left", expand=True, fill="both")
        right.pack(side="right", expand=True, fill="both")

        box3.pack(expand=True, fill="both", padx=10, pady=10)
        box1.pack(expand=True, fill="both", padx=10, pady=10)
        box2.pack(expand=True, fill="both", padx=10, pady=10)

        label1.pack()
        label2.pack()
        label3.pack()


    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining

        if self.remaining <= 0:
            self.label.configure(text="time's up!")
        else:
            self.label.configure(text="%d" % self.remaining)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)

if __name__ == "__main__":
    app = split_screen()
    app.screen()
    app.mainloop()

最简单的解决方案——可以说是最好的——是创建一个代表单个计时器的 class。然后,您可以创建任意数量的实例。这正是 classes 的用途:将某些行为封装在对象中。

由于您使用标签来显示时间,您可以让计时器 class 继承自 Label,或者您可以在创建计时器时传入标签。

这是一个继承自 tk.Label 的示例:

import tkinter as tk

class TimerLabel(tk.Label):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.after_id = None
        self.remaining = None

    def countdown(self, seconds):
        if seconds <= 0:
            self.configure(text="Time's up!")
        else:
            self.configure(text=f"{seconds}")
            self.after_id = self.after(1000, self.countdown, seconds-1)

    def stop(self):
        if self.after_id:
            self.configure(text="cancelled")
            self.after_cancel(self.after_id)

使用上面的 class 定义,您可以创建任意数量的计时器小部件。这是创建三个的示例:

root = tk.Tk()

timer1 = TimerLabel(root, width=10)
timer2 = TimerLabel(root, width=10)
timer3 = TimerLabel(root, width=10)

timer1.pack(side="top", padx=20)
timer2.pack(side="top", padx=20)
timer3.pack(side="top", padx=20)

timer1.countdown(30)
timer2.countdown(20)
timer3.countdown(10)

root.mainloop()

我做了多项更改: 正如 Bryan Oakley 所说,不要创建 Tk() 的多个实例。您也不需要导入 tkinter 两次。

import tkinter as tk


class split_screen(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.geometry("650x700")
        self.configure(bg='black')
        self.title("test")
        left = tk.Frame(self, borderwidth=200, relief="solid")
        right = tk.Frame(self, borderwidth=20, relief="solid")
        box1 = tk.Frame(left, borderwidth=2, relief="solid")
        box2 = tk.Frame(left, borderwidth=2, relief="solid")
        box3 = tk.Frame(right, borderwidth=2, relief="solid")

        label1 = tk.Label(box3, text="winner's" + "\n\n\n" + "Player 1",fg= "black", bg = "red", font = "david 18 bold underline")
        label2 = tk.Label(box1, text="Computer 1",fg = "black", bg = "red", font= "david 18 bold underline")
        label3 = tk.Label(box2, text="Computer 2",fg = "black", bg = "red", font= "david 18 bold underline")

        clock1 = Clock(box1, 1000)
        clock2 = Clock(box2, 2000)
        clock3 = Clock(box3, 1300)

        left.pack(side="left", expand=True, fill="both")
        right.pack(side="right", expand=True, fill="both")

        box3.pack(expand=True, fill="both", padx=10, pady=10)
        box1.pack(expand=True, fill="both", padx=10, pady=10)
        box2.pack(expand=True, fill="both", padx=10, pady=10)

        label1.pack()
        label2.pack()
        label3.pack()


class Clock():
    def __init__(self, frame, count):
        self.frame = frame
        self.label = tk.Label(frame, text="", width=10,fg="black", bg ="red", font="david 18 bold underline")
        self.label.pack()
        self.remaining = 0
        self.countdown(count)
        frame.configure(background="black")

    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining

        if self.remaining <= 0:
            self.label.configure(text="time's up!")
        else:
            self.label.configure(text="%d" % self.remaining)
            self.remaining = self.remaining - 1
            self.frame.after(1000, self.countdown)

if __name__ == "__main__":
    app = split_screen()
    app.mainloop()