jonny
jonny

Reputation: 33

How to add timer to each divide screen in tkinter

I created one window and split it into 3 different windows. I want to add a clock to each screen, that does not depend on the other clocks. my code opens 2 window- one is the timer and the second one is the window that i split to 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()

Upvotes: 0

Views: 338

Answers (2)

Phoenixo
Phoenixo

Reputation: 2133

I did multiple changes : As Bryan Oakley said, don't create multiple instances of Tk(). You also don't need to import tkinter twice.

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

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386325

The simplest solution -- and arguably the best -- is to create a class that represents a single timer. You can then make as many instances as you want. This is precisely the sort of thing that classes are for: to encapsulate some behavior inside an object.

Since you're using a label to display the time, you can either have the timer class inherit from Label, or you can pass the label in when creating the timer.

Here's an example which inherits from 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)

Using the above class definition, you can create as many timer widgets as you want. Here's an example that creates three:

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

Upvotes: 0

Related Questions