Hello Friends, I am working on my school project. But I have a problem. I am trying to make a timer with a counter. I am trying to make a reset button for timer as well as for counter but I am not able to make it. I am new to programing world so please help me.
I have visited many sites but they are making the timer in classes an I am not good at programing in classes.
Below I am giving my code. If any any one knows how to do it please answer me.
*Note- Giving counter value is important.
from tkinter import *
import threading
import time
import tkinter
root = Tk()
root.title("Scada Clone ")
#width1 = root.winfo_screenwidth()
#height1 = root.winfo_screenheight()
root.geometry("800x480")
########################################################################################################################
pauseEvent = threading.Event()
global is_asleep
r=True
def important_function(e):
count = int(CountEntry.get())
i = 0
while (i<count):
event_is_set = pauseEvent.wait()
###########################time1####################################
hour.set(hourEntry1.get())
minute.set(minuteEntry1.get())
second.set(secondEntry1.get())
hourEntry.configure(textvariable=hour)
minuteEntry.configure(textvariable=minute)
secondEntry.configure(textvariable=second)
########################################time2###############################################
hourt2.set(hourEntry2.get())
minutet2.set(minuteEntry2.get())
secondt2.set(secondEntry2.get())
hourEntryt2.configure(textvariable=hourt2)
minuteEntryt2.configure(textvariable=minutet2)
secondEntryt2.configure(textvariable=secondt2)
try:
# the input provided by the user is
# stored in here :temp
temp = int(hour.get()) * 3600 + int(minute.get()) * 60 + int(second.get())
except:
print("Please input the right value")
while temp > -1:
event_is_set = pauseEvent.wait()
# divmod(firstvalue = temp//60, secondvalue = temp%60)
mins, secs = divmod(temp, 60)
# Converting the input entered in mins or secs to hours,
# mins ,secs(input = 110 min --> 120*60 = 6600 => 1hr :
# 50min: 0sec)
hours = 0
if mins > 60:
# divmod(firstvalue = temp//60, secondvalue
# = temp%60)
hours, mins = divmod(mins, 60)
# using format () method to store the value up to
# two decimal places
hour.set("{0:2d}".format(hours))
minute.set("{0:2d}".format(mins))
second.set("{0:2d}".format(secs))
# updating the GUI window after decrementing the
# temp value every time
root.update()
time.sleep(1)
# when temp value = 0; then a messagebox pop's up
# with a message:"Time's up"
if (temp == 0):
print("Hi")
# after every one sec the value of temp will be decremented
# by one
temp -= 1
############################### Timer 2 ######################################################
try:
# the input provided by the user is
# stored in here :temp
temp1 = int(hourt2.get()) * 3600 + int(minutet2.get()) * 60 + int(secondt2.get())
except:
print("Please input the right value")
while temp1 > -1:
event_is_set = pauseEvent.wait()
# divmod(firstvalue = temp//60, secondvalue = temp%60)
mins1, secs1 = divmod(temp1, 60)
# Converting the input entered in mins or secs to hours,
# mins ,secs(input = 110 min --> 120*60 = 6600 => 1hr :
# 50min: 0sec)
hours1 = 0
if mins1 > 60:
# divmod(firstvalue = temp//60, secondvalue
# = temp%60)
hours1, mins1 = divmod(mins1, 60)
# using format () method to store the value up to
# two decimal places
hourt2.set("{0:2d}".format(hours1))
minutet2.set("{0:2d}".format(mins1))
secondt2.set("{0:2d}".format(secs1))
# updating the GUI window after decrementing the
# temp value every time
root.update()
time.sleep(1)
# when temp value = 0; then a messagebox pop's up
# with a message:"Time's up"
if (temp1 == 0):
print("Hi")
# after every one sec the value of temp will be decremented
# by one
temp1 -= 1
tfcoa = open("counted.txt", "r") # or tf = open(tf, 'r')
datacoa = tfcoa.read()
if datacoa == "":
dataco11 = "00"
else:
dataco11 = datacoa
i = i + 1
Countlabel.configure(text=i)
root.update()
def pause():
# pause the execution
# e.wait()
pauseEvent.clear()
def resume():
pauseEvent.set()
def stop():
# stop the execution
try:
print('Stop')
pauseEvent.clear()
root.quit()
sys.exit("some error message")
except:
print('Error Occured')
def start():
t1 = threading.Thread(target=important_function,
args=(pauseEvent,))
t1.daemon = True
t1.start()
pauseEvent.set()
############################################################Time1 Input Code#############################################
Hours = Label(root, width=10, font=("Arial", 25, ""),text="H",fg='#e75480',bg="black")
Hours.grid(row=2, column=1)
Minute = Label(root, width=10, font=("Arial", 25, ""),text="M",fg='#e75480',bg="black")
Minute.grid(row=3, column=1)
Seconds = Label(root, width=10, font=("Arial", 25, ""),text="Sec",fg='#e75480',bg="black")
Seconds.grid(row=4, column=1)
time1=Label(root, font=("Arial", 30, ""),text='Timer1',justify=CENTER,pady=5,padx=5,fg='#e75480',bg="black")
time1.grid(row=0,column=2)
def only_numbers(char):
return char.isdigit()
validation = root.register(only_numbers)
hour1 = StringVar()
minute1 = StringVar()
second1 = StringVar()
# Use of Entry class to take input from the user
hourEntry1 = Entry(root, width=10, font=("Arial", 20, ""), validate="key", validatecommand=(validation, '%S'))
hourEntry1.grid(row=2, column=2)
hourEntry1.insert(END,"00")
minuteEntry1 = Entry(root, width=10, font=("Arial", 20, ""), validate="key", validatecommand=(validation, '%S'))
minuteEntry1.grid(row=3, column=2)
minuteEntry1.insert(END,"00")
secondEntry1 = Entry(root, width=10, font=("Arial", 20, ""), validate="key", validatecommand=(validation, '%S'))
secondEntry1.grid(row=4, column=2)
secondEntry1.insert(END,"00")
###############################################################End Of Time1 Input Code##############################
hour = StringVar()
minute = StringVar()
second = StringVar()
# setting the default value as 0
hour.set("00")
minute.set("00")
second.set("00")
# Use of Entry class to take input from the user
hourEntry = Label(root, width=3, font=("Arial", 30, ""),
textvariable=hour,fg="green",bg="black")
hourEntry.grid(row=2, column=3)
minuteEntry = Label(root, width=3, font=("Arial", 30, ""),
textvariable=minute,fg="green",bg="black")
minuteEntry.grid(row=3, column=3)
secondEntry = Label(root, width=3, font=("Arial", 30, ""),
textvariable=second,fg="green",bg="black")
secondEntry.grid(row=4, column=3)
#######################################################Timer 2 ####################################################
time2=Label(root, font=("Arial", 30, ""),text='Timer2',justify=CENTER,pady=5,padx=5,fg='#e75480',bg="black")
time2.grid(row=5,column=2)
Hours = Label(root, width=10, font=("Arial", 25, ""),
text="H",fg='#e75480',bg="black")
Hours.grid(row=6, column=1)
Minute = Label(root, width=10, font=("Arial", 25, ""),
text="M",fg='#e75480',bg="black")
Minute.grid(row=7, column=1)
Seconds = Label(root, width=10, font=("Arial", 25, ""),
text="Sec",fg='#e75480',bg="black")
Seconds.grid(row=8, column=1)
def only_numbers(char):
return char.isdigit()
validation = root.register(only_numbers)
# Use of Entry class to take input from the user
hourEntry2 = Entry(root, width=10, font=("Arial", 20, ""), validate="key", validatecommand=(validation, '%S'))
hourEntry2.grid(row=6, column=2)
hourEntry2.insert(END,"00")
minuteEntry2 = Entry(root, width=10, font=("Arial", 20, ""), validate="key", validatecommand=(validation, '%S'))
minuteEntry2.grid(row=7, column=2)
minuteEntry2.insert(END,"00")
secondEntry2 = Entry(root, width=10, font=("Arial", 20, ""), validate="key", validatecommand=(validation, '%S'))
secondEntry2.grid(row=8, column=2)
secondEntry2.insert(END,"00")
hourt2 = StringVar()
minutet2 = StringVar()
secondt2 = StringVar()
# setting the default value as 0
hourt2.set("00")
minutet2.set("00")
secondt2.set("00")
# Use of Entry class to take input from the user
hourEntryt2 = Label(root, width=3, font=("Arial", 30, ""),
textvariable=hourt2,fg='green',bg="black")
hourEntryt2.grid(row=6, column=3)
minuteEntryt2 = Label(root, width=3, font=("Arial", 30, ""),
textvariable=minutet2,fg='green',bg="black")
minuteEntryt2.grid(row=7, column=3)
secondEntryt2 = Label(root, width=3, font=("Arial", 30, ""),
textvariable=secondt2,fg='green',bg="black")
secondEntryt2.grid(row=8, column=3)
################################################ Counter ########################################################
Count = Label(root, width=7, font=("Arial", 30, ""),text="Count:",justify=CENTER,pady=5,padx=5,bg='black',fg='#e75480')
Count.grid(row=2, column=5)
CountEntry = Entry(root, width=10, font=("Arial", 30, ""))
CountEntry.grid(row=3, column=5)
CountEntry.insert(END,"00")
Countlabel = Label(root, font=("Arial", 30, ""), text="00", justify=CENTER,fg="green",bg="black")
Countlabel.grid(row=4, column=5)
############################################################### Buttons ##########################################
def reset():
Countlabel.configure(text="0")
root.update()
startButton = Button(root, text="Start",justify=CENTER, bd=0, command=start)
startButton.grid(row=1, column=0)
pauseButton = Button(root, text="Pause",justify=CENTER, bd=0, command=pause)
pauseButton.grid(row=2, column=0)
stopButton = Button(root, text="Cancel",justify=CENTER, bd=0, command=stop)
stopButton.grid(row=4, column=0)
resumeButtom = Button(root, text="Resume",justify=CENTER, bd=0, command=resume)
resumeButtom.grid(row=3, column=0)
resetButtom = Button(root, text="Reset",justify=LEFT, command=reset,)
resetButtom.grid(row=5, column=0)
########################################################################################################################
root.configure(bg='black')
root.mainloop()
Here there are two timers which actives alternatly that is one after the another. When t1 timer is complete t2 timer starts and after complition of t2 timer time counter takes +1 count. This process goes on until counter completes its counts. Pause and Resume buttons are also given to to pause and resume the timer.