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
The user can interact with other widgets while the timer runs. Example: - 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()
5 2146
Nvrmind....answered my own question.
bvdet 2,851
Expert Mod 2GB
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. - 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()
bvdet 2,851
Expert Mod 2GB
OK Jory - What was the answer?
: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?
bvdet 2,851
Expert Mod 2GB
The user can interact with other widgets while the timer runs. Example: - 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()
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John Salerno |
last post by:
Ok, I made some changes and want to see what you guys think, as far as
syntax and logic, etc. What I want it to do is show the number of
minutes remaining each minute, and then beep at the end. Is...
|
by: Mr Newbie |
last post by:
Im going to be looking to do this exam fairly soon, but ive done a couple of
practice tests and found them to be a bit tricky in as much as you have to
REALLY READ the questions carefully else they...
|
by: rojavenkat81 |
last post by:
Hi All,
Please help me regarding php program for convert video file to .flv file.
I searched in many sites but i got only tool to convert video file to .flv file.But I want convert...
|
by: Nirav Amin |
last post by:
Create a 2-D array containing the data :
Pakistan Sweden France Iran China
Japan Russia China Italy China
China ...
|
by: John |
last post by:
Hello,
I'm brand new to Javascript and would like to know what is the best
program that I can use to learn JavaScript. I would like something
with syntax highlighting if possible.
Any help...
|
by: mrcw |
last post by:
I'm trying to make a timer which is a reusable module. Is this possible? I would like to pass 1 argument to the timer. The argument is the delay that the main program waits before it continues.
...
|
by: Colt |
last post by:
I have this program that an associate will input their ID# then it prints back saying (your associate ID# is XXX) then it will state what time it they clocked in/out--Ex: Your ID# is 101 and you left...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |