Home > database >  Python tKinter: How to pause countdown timer
Python tKinter: How to pause countdown timer

Time:05-22

I made a countdown timer that starts whenever I press on the space key on my keyboard, but the problem is that I can't do anything on the program until the timer ends. I want make it pause when the space key is pressed a second time.

The countdown timer that I made works in a while loop that ends when the timer reach 0, so the program waits until the loop ends before doing anything else, even if I want to stop the timer I can't do it while it's running.

Here's the code

from tkinter import *
from tkinter import ttk
import tkinter as tk
from PIL import ImageTk, Image


def StartTimer():
    if (root.turn % 2) == 0: #Turn to white
        root.number = '1'
        root.color = 'white'
    else: #Turn to black
        root.number = '2'
        root.color = 'black'
    doTimer()

def doTimer():
    root.time = root.minute *60   root.second
    root.turn = root.turn 1

    number = root.number

    root.rndsquare1.configure(image=root.green)
    root.timer1.configure(bg='#1C953D')
    root.white.configure(bg='#1C953D')
    r=0
    while r < root.time:
        root.update_idletasks()
        root.after(1000)
        root.second = root.second - 1
        if root.second == -1:
            root.minute = root.minute -1
            root.second = 59

        root.time1 = ''
        if len(str(root.minute)) == 1:
            root.time1 = '0'   str(root.minute)
        else:
            root.time1 = str(root.minute)
        if len(str(root.second)) == 1:
            root.time1 = root.time1   ':'   '0'   str(root.second)
        else:
            root.time1 = root.time1   ':'   str(root.second)
        
        root.timer1.configure(text=root.time1)
        r=r 1
    
    root.timer1.configure(bg='#454545')
    root.white.configure(bg='#454545')
    root.rndsquare1.configure(image=root.grey)



class root(Tk):
    def __init__(self):
        super(root, self).__init__()

        self.title("Chess Clock")
        self.minsize(1539,600)
        self.windowBG = '#313131'
        self.state('zoomed')
        self.configure(bg=self.windowBG)

        self.CreateWindow()

    def CreateWindow(self):
        self.grey = ImageTk.PhotoImage(Image.open(r"D:\Users\Jean Paul\OneDrive\Programming\Programs\Prog 6 - Chess Clock\bg square grey.png"))
        self.green = ImageTk.PhotoImage(Image.open(r"D:\Users\Jean Paul\OneDrive\Programming\Programs\Prog 6 - Chess Clock\bg square green.png"))
        self.turn=0

        self.rndsquare1 = Label(self, image=self.grey, borderwidth=0)
        self.rndsquare1.place(x=65, y=120)
        self.rndsquare2 = Label(self, image=self.grey, borderwidth=0)
        self.rndsquare2.place(x=809, y=120)

        self.bind('<space>',lambda event:StartTimer())
        self.createTimers()



    def createTimers(self):
        self.minute = 1
        self.second = 5

        self.time1 = ''
        if len(str(self.minute)) == 1:
            self.time1 = '0'   str(self.minute)
        else:
            self.time1 = str(self.minute)
        if len(str(self.second)) == 1:
            self.time1 = self.time1   ':'   '0'   str(self.second)
        else:
            self.time1 = self.time1   ':'   str(self.second)

        self.time2 = ''
        if len(str(self.minute)) == 1:
            self.time2 = '0'   str(self.minute)
        else:
            self.time2 = str(self.minute)
        if len(str(self.second)) == 1:
            self.time2 = self.time2   ':'   '0'   str(self.second)
        else:
            self.time2 = self.time2   ':'   str(self.second)

        self.timer1 = Label(self, text=self.time1, bg='#454545', fg='white', font ="Gadugi 40 bold")
        self.timer1.place(x=330, y=420)
        
        self.timer2 = Label(self, text=self.time2, bg='#454545', fg='white', font ="Gadugi 40 bold")
        self.timer2.place(x=1080, y=420)

        self.white = Label(self, text='White', bg='#454545', fg='white', font ="Gadugi 40 bold")
        self.white.place(x=325, y=160)

        self.black = Label(self, text='Black', bg='#454545', fg='white', font ="Gadugi 40 bold")
        self.black.place(x=1075, y=160)



root=root()
root.mainloop()

image of the clock while running

after the first CTRL q you'll get the results - a second time CTRL q closes your window:

image after first ctrl q

This can be better structured regarding UI/logic stuff - but it works as proof of concept.

  • Related