I'm trying to write a python code including GUI (tkinter) that runs 2 or even more timers at the same time with different actions/commands, but with the same "Start" button. The problem in my code below is the 2 timers are created but they are sequentially running. Can I create 2 timers with different time set running synchronously.
import time
from tkinter import *
from tkinter import messagebox
#Create interface#
root = Tk()
root.geometry("500x500")
root.title("Countdown Timer")
#declare variables for Timer1
hrString1 = StringVar()
minuteString1 = StringVar()
secString1 = StringVar()
#Set String to default values
hrString1.set("00")
minuteString1.set("00")
secString1.set("00")
#Get User Input
hourTextBox1 = Entry(root, width=3, font=("Calibri", 20, ""), textvariable=hrString1).place(x=170, y=100)
minuteTextBox1 = Entry(root, width=3, font=("Calibri", 20, ""), textvariable=minuteString1).place(x=220, y=100)
secondTextBox1 = Entry(root, width=3, font=("Calibri", 20, ""), textvariable=secString1).place(x=270, y=100)
#declare variables
hrString2 = StringVar()
minString2 = StringVar()
secString2 = StringVar()
#Set String to default values
hrString2.set("00")
minString2.set("00")
secString2.set("00")
#Get User Input
hourTextBox2 = Entry(root, width=3, font=("Calibri", 20, ""), textvariable=hrString2).place(x=170, y=180)
minuteTextBox2 = Entry(root, width=3, font=("Calibri", 20, ""), textvariable=minString2).place(x=220, y=180)
secondTextBox2 = Entry(root, width=3, font=("Calibri", 20, ""), textvariable=secString2).place(x=270, y=180)
def runTimer1():
try:
clockTime = int(hrString1.get())*3600 + int(minuteString1.get())*60 + int(secString1.get())
except:
print("Incorrect values")
while(clockTime > -1):
totalMinutes, totalSeconds = divmod(clockTime, 60)
totalHours = 0 #initialize
if(totalMinutes>60):
totalHours, totalMinutes = divmod(totalMinutes, 60)
hrString1.set("{0:2d}".format(totalHours))
minuteString1.set("{0:2d}".format(totalMinutes))
secString1.set("{0:2d}".format(totalSeconds))
#update the interface with each iteration
root.update()
time.sleep(1)
#time expires
if(clockTime == 0):
messagebox.showinfo("", "time One has expired!") #Run Command 1
clockTime -= 1
def runTimer2():
try:
clockTime2 = int(hrString2.get())*3600 + int(minString2.get())*60 + int(secString2.get())
except:
print("Incorrect values")
while(clockTime2 > -1):
totalMinutes, totalSeconds = divmod(clockTime2, 60)
totalHours = 0 #initialize
if(totalMinutes>60):
totalHours, totalMinutes = divmod(totalMinutes, 60)
hrString2.set("{0:2d}".format(totalHours))
minString2.set("{0:2d}".format(totalMinutes))
secString2.set("{0:2d}".format(totalSeconds))
#update the interface with each iteration
root.update()
time.sleep(1)
#time expires
if(clockTime2 == 0):
messagebox.showinfo("", "time Two has expired!") #Run Command 2
clockTime2 -= 1
setTimeButton = Button(root, text='START', bd='5', command=lambda: [runTimer1(), runTimer2()]).place(relx=0.5, rely=0.8, anchor=CENTER)
#Keep Loop
root.mainloop()