pythontkinterscreensplit-screen-multitasking

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

Solution

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