0

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

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

2 回答 2

0

我做了多项更改:正如 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()
于 2020-01-08T15:47:42.990 回答
0

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

由于您使用标签来显示时间,因此您可以让计时器类继承自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)

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

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()
于 2020-01-08T15:43:13.030 回答