I will be using python 3 (comp is currently broken so I can't test examples) btw. Do I need to create the timer function as a separate thread and create a event-driven interruption in the main loop which reacts to the timer? Or is there a simpler way to create the timer INSIDE the main loop that I'm too retarded to think of? :P
If I want to add a timer to my program (Timed practice exam)
Collapse
X
-
Tags: None
-
-
I just happen to have a Tkinter script demonstrating a timer application. You should be able to do what you want in the main loop.Code:import Tkinter from Tkconstants import * import time textFont1 = ("Arial", 16, "bold") class Timer(Tkinter.Frame): def __init__(self, master): self.master = master buttonFrame = Tkinter.Frame(master) self.btnList = [] for i, name in enumerate(["Start", "Stop", "Exit"]): def handler(i=i): return self.manage_timer(i) btn = Tkinter.Button(buttonFrame, text=name, padx=5, pady=5, bd=4, bg='#ff0', fg="#00f", activebackground = "#00f", activeforeground = "#ff0", font=textFont1, relief='raised', command=handler) btn.pack(side="left", fill=BOTH, expand=1) self.btnList.append(btn) buttonFrame.pack(fill=BOTH, expand=1) self.start() def start(self): Tkinter.Frame.__init__(self, self.master) self.pack(fill=BOTH, expand=1) self.clockVar = Tkinter.StringVar(self.master) self.clockLabel = Tkinter.Label(self, textvariable=self.clockVar, relief="raised", font=textFont1, bd=3, bg='#ffffff000', fg="#000000fff", activebackground = "#000000fff", activeforeground = "#ffffff000", takefocus=1, padx=3, pady=3, width=16) self.clockLabel.pack(fill=BOTH, expand=1) self.clockVar.set("0.000") self.btnList[1].configure(state=DISABLED) def update_time(self): self.clockVar.set("%0.3f" % (time.time()-self.starttime)) if self.active: self.after(10, self.update_time) else: self.clockVar.set("Elapsed time: %0.3f"% (time.time()-self.starttime)) def manage_timer(self, idx): if idx == 0: self.btnList[0].configure(state=DISABLED) self.btnList[1].configure(state=NORMAL) self.active = True self.starttime = time.time() self.update_time() elif idx == 1: self.btnList[0].configure(state=NORMAL) self.btnList[1].configure(state=DISABLED) self.active = False elif idx == 2: self.master.destroy() if __name__ == "__main__": root = Tkinter.Tk() app = Timer(root) root.mainloop()Last edited by bvdet; Jan 14 '12, 02:03 AM.Comment
-
:P Well...it was the wrong answer....I thought I would need to use a thread! So since the timer is created as an instance of an object instead of just as a function, it allows me to run the timer at the same time as determining questions to ask and taking input from the user, as if I was using a separate thread?Comment
-
The user can interact with other widgets while the timer runs. Example:
Code:import Tkinter from Tkconstants import * import time textFont1 = ("Arial", 16, "bold") class EntryWidget(Tkinter.Entry): def __init__(self, master, initial=""): Tkinter.Entry.__init__(self, master=master) self.value = Tkinter.StringVar() self.config(textvariable=self.value, width=20, relief="ridge", font=textFont1, bg="#ddd", fg="#e00", justify='center') self.pack(fill=BOTH, expand=1) self.value.set(initial) class Timer(Tkinter.Frame): def __init__(self, master): self.master = master buttonFrame = Tkinter.Frame(master) self.btnList = [] for i, name in enumerate(["Start", "Stop", "Exit"]): def handler(i=i): return self.manage_timer(i) btn = Tkinter.Button(buttonFrame, text=name, padx=5, pady=5, bd=4, bg='#ff0', fg="#00f", activebackground = "#00f", activeforeground = "#ff0", font=textFont1, relief='raised', command=handler) btn.pack(side="left", fill=BOTH, expand=1) self.btnList.append(btn) buttonFrame.pack(fill=BOTH, expand=1) self.start() def start(self): Tkinter.Frame.__init__(self, self.master) self.pack(fill=BOTH, expand=1) self.clockVar = Tkinter.StringVar(self.master) self.clockLabel = Tkinter.Label(self, textvariable=self.clockVar, relief="raised", font=textFont1, bd=3, bg='#ffffff000', fg="#000000fff", activebackground = "#000000fff", activeforeground = "#ffffff000", takefocus=1, padx=3, pady=3, width=20) self.clockLabel.pack(fill=BOTH, expand=1) self.clockVar.set("0.000") self.btnList[1].configure(state=DISABLED) self.w = EntryWidget(self, "Enter Text") self.w.bind(sequence="<KeyRelease>", func=self.lower) def lower(self, event): event.widget.value.set(event.widget.value.get().lower()) def update_time(self): self.clockVar.set("%0.3f" % (time.time()-self.starttime)) if self.active: self.after(10, self.update_time) else: self.clockVar.set("Elapsed time: %0.3f"% (time.time()-self.starttime)) def manage_timer(self, idx): if idx == 0: self.btnList[0].configure(state=DISABLED) self.btnList[1].configure(state=NORMAL) self.active = True self.starttime = time.time() self.update_time() elif idx == 1: self.btnList[0].configure(state=NORMAL) self.btnList[1].configure(state=DISABLED) self.active = False elif idx == 2: self.master.destroy() if __name__ == "__main__": root = Tkinter.Tk() app = Timer(root) root.mainloop()Last edited by bvdet; Jan 14 '12, 02:03 AM.Comment
Comment