[Tkinter-discuss] Problem looping
Brill IanT
woswwf at yahoo.co.uk
Mon May 2 18:41:50 CEST 2005
Hi all!
I was wondering if anyone could help me out.
I wrote a little alarm clock program in Python, and now want to incorporate it into a Tkinter frame. It's all going quite well, but this is my first experience with Tk and I've hit a bit of a wall. One of the button's commands refers to a method (called 'act') which has a while loop within it (keeps track of the time and ought to display it in the Tk frame). But the problem is the while loop is interupting the root mainloop, so the labels will not update, and freezes until the while loop is over.
Does anyone have any suggestions to get round this?
Here's the program:
import time, winsound
from Tkinter import *
class App:
def __init__(self,master):
self.master = master
frame = Frame(master,bd=6,relief=RIDGE)
frame.pack()
frame2 = Frame(master)
frame2.pack()
self.list = ["9","8","7","6","5","4","3","2","1","0"]
self.stringv = StringVar()
self.stringv.set("0")
self.stringv2 = StringVar()
self.stringv2.set("0")
self.stringv3 = StringVar()
self.stringv3.set("0")
self.stringv4 = StringVar()
self.stringv4.set("0")
self.lab1 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv).pack(side=LEFT)
self.b1 = Button(frame2,text="H",width=3,command=self.change)
self.b1.pack(side=LEFT)
self.lab2 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv2).pack(side=LEFT)
self.b2 = Button(frame2,text="h",width=3,command=self.change2)
self.b2.pack(side=LEFT)
self.lab3 = Label(frame,text=":",font=("helvica",22),
bg="red",).pack(side=LEFT)
self.lab4 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv3).pack(side=LEFT)
self.b3 = Button(frame2,text="M",width=3,command=self.change3)
self.b3.pack(side=LEFT)
self.lab5 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv4).pack(side=LEFT)
self.b4 = Button(frame2,text="m",width=3,command=self.change4)
self.b4.pack(side=LEFT)
self.set_button = Button(master,text="Set",width=20,
command=self.act).pack()
def change(self):
if int(self.stringv2.get()) < 3:
self.list1 = self.list[7:]
i = self.list1.index(self.stringv.get())
self.stringv.set(self.list1[i-1])
else:
self.list1 = self.list[8:]
i = self.list1.index(self.stringv.get())
self.stringv.set(self.list1[i-1])
def change2(self):
self.list2 = self.list[6:]
if self.stringv.get() == "2":
i = self.list2.index(self.stringv2.get())
self.stringv2.set(self.list2[i-1])
else:
i = self.list.index(self.stringv2.get())
self.stringv2.set(self.list[i-1])
def change3(self):
self.list3 = self.list[4:]
i = self.list3.index(self.stringv3.get())
self.stringv3.set(self.list3[i-1])
def change4(self):
i = self.list.index(self.stringv4.get())
self.stringv4.set(self.list[i-1])
def act(self):
self.ring = self.stringv.get()+self.stringv2.get()+":"\
+self.stringv3.get()+self.stringv4.get()
self.track = [None]
self.c = self.clock()
self.stringv.set(self.c[0])
self.stringv2.set(self.c[1])
self.stringv3.set(self.c[3])
self.stringv4.set(self.c[4])
while 1:
self.c = self.clock()
if self.c != self.ring:
if self.c == self.track[-1]: pass
else:
self.stringv.set(self.c[0])
self.stringv2.set(self.c[1])
self.stringv3.set(self.c[3])
self.stringv4.set(self.c[4])
self.track.append(self.c)
else: winsound.PlaySound("rooster.wav", winsound.SND_LOOP)
def clock(self):
t1 = time.localtime()[3:5]
if t1[0] in range(10): tm1 = '0'+ str(t1[0])
else: tm1 = str(t1[0])
if t1[1] in range(10): tm2 = '0'+ str(t1[1])
else: tm2 = str(t1[1])
tm3 = tm1 + ":" + tm2
return tm3
if __name__ == "__main__":
root=Tk()
root.title("")
root.geometry("100x97+300+200")
App(root)
root.mainloop()
Thanks in advance.
Send instant messages to your online friends http://uk.messenger.yahoo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050502/77610d5d/attachment.htm
More information about the Tkinter-discuss
mailing list