Tkinter Countdown Timer problems.

Thread Solved

Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Tkinter Countdown Timer problems.

 
0
  #1
Feb 7th, 2007
Hi. I've just registered, but I'm not exactly new to here (i've been lurking as a guest). I'm trying to make a countdown timer based on a class that I created, which accepts hours, minutes, and seconds as parameters and counts them down to zero, will displaying them in a Tkinter window.

Now, I did start on it using Tkinter, but the Tkinter window just never appears (for some reason, I suspect this has something to do with the time.sleep() i use in my aggregate function, because everything else seems fine and when I the aggregate() function call, the window runs normally.

I managed to create a temporary fix using livewires, that instead of using time.sleep(), uses the mainloop() cycles to decide when to decrement a second (mainloop in livewires do a certain amount of cycles per second). The problem with this is that it isn't always accurate. That is, sometimes it goes too fast, and sometimes it goes too slow with the exact same FPS settings (I suspect this has to do with my CPU load or something...i'm not very skilled and I don't know a lot about these things).
Another problem with the livewires method is that while in Tkinter, I could just destroy() the timer window when the timer is through and call back the main window(I want the timer to quit, and open back the start screen when it is through), with this livewires version it just locks up when i tell it screen.quit() (The destroy() function only wipes all the objects off. Heres the code (so sorry for lack of commenting...i was supposed to comment it last night, but got sidetracked and now im at work and dont have much time. I'll add comments if you cant understand the code):


#Te-je Rodgers
#Counter module

from livewires import games, color


class Counter(games.Sprite):
"""My counter is a sprite."""
def __init__(self, screen, x, y,image, passer):
self.screen = screen
self.init_sprite(screen = screen, x = x, y = y, image=image)
self.name=passer[0]
timer=passer[1]
self.hh=timer[0]
self.mm=timer[1]
self.ss=timer[2]
self.count=0
self.col=color.blue
games.Text(screen=screen, x=200, y=20, text=self.name, color = color.white, size=50)
games.Text(screen=screen, x=200, y=180, text="Press ENTER or F1 to stop",
color=color.white, size=20)


self.display_message()


def close_timer(self):

if self.hh<0:
import last
else:
self.screen.quit()


def good(self):
x=0



def display_message(self):
games.Message(screen = self.screen, x=200, y=100,
text=str(self.hh)+" : "+str(self.mm)+" : "+str(self.ss), size=100, after_death=self.good(),
color=self.col, lifetime=75)




def decrease_seconds(self):
self.ss-=1
if self.hh>=0:
if self.ss<0:
self.ss=59
self.mm-=1
if self.mm<0:
self.mm=59
self.hh-=1
if self.hh<0:
self.close_timer()
else:
self.display_message()
else:
self.display_message()
else:
self.display_message()





def moved(self):
self.count+=1
if self.screen.is_pressed(games.K_RETURN) or self.screen.is_pressed(games.K_F1) or self.screen.is_pressed(games.K_KP_ENTER):
self.close_timer()
if self.hh==0 and self.mm==0 and self.ss==6:
self.col=color.red
if self.count==75:
self.decrease_seconds()
self.count=0



def start(passer):
root32=games.Screen(400, 200)
sprite_image=games.load_image("img.bmp")
back=games.load_image("box2.jpg")
root32.set_background(back)
x=Counter(root32, 296, 139, sprite_image, passer)
root32.mainloop(50)



the parameter "passer" in in this format:
("string",(int_hh, int_mm, int_ss))
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Tkinter Countdown Timer problems.

 
0
  #2
Feb 7th, 2007
Please use the [code=python] and [/code] tag pair to enclose your python code. Otherwise you lose all your indentations and you code becomes unusable!

With the housekeeping out of the way, "Welcome to the Python forum!"
Last edited by vegaseat; Feb 7th, 2007 at 2:28 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Tkinter Countdown Timer problems.

 
0
  #3
Feb 7th, 2007
humblest apologies.


Livewires based timer:
  1. #Te-je Rodgers
  2. #Counter module
  3. from livewires import games, color
  4.  
  5. class Counter(games.Sprite):
  6. """My counter is a sprite."""
  7. def __init__(self, screen, x, y,image, passer):
  8. self.screen = screen
  9. self.init_sprite(screen = screen, x = x, y = y, image=image)
  10. self.name=passer[0]
  11. timer=passer[1]
  12. self.hh=timer[0]
  13. self.mm=timer[1]
  14. self.ss=timer[2]
  15. self.count=0
  16. self.col=color.blue
  17. games.Text(screen=screen, x=200, y=20, text=self.name, color = color.white, size=50)
  18. games.Text(screen=screen, x=200, y=180, text="Press ENTER or F1 to stop",
  19. color=color.white, size=20)
  20.  
  21. self.display_message()
  22.  
  23. def close_timer(self):
  24.  
  25. if self.hh<0:
  26. import last
  27. else:
  28. self.screen.quit()
  29.  
  30.  
  31. def good(self):
  32. x=0
  33. def display_message(self):
  34. games.Message(screen = self.screen, x=200, y=100,
  35. text=str(self.hh)+" : "+str(self.mm)+" : "+str(self.ss), size=100, after_death=self.good(),
  36. color=self.col, lifetime=75)
  37.  
  38. def decrease_seconds(self):
  39. self.ss-=1
  40. if self.hh>=0:
  41. if self.ss<0:
  42. self.ss=59
  43. self.mm-=1
  44. if self.mm<0:
  45. self.mm=59
  46. self.hh-=1
  47. if self.hh<0:
  48. self.close_timer()
  49. else:
  50. self.display_message()
  51. else:
  52. self.display_message()
  53. else:
  54. self.display_message()
  55. def moved(self):
  56. self.count+=1
  57. if self.screen.is_pressed(games.K_RETURN) or self.screen.is_pressed(games.K_F1) or self.screen.is_pressed(games.K_KP_ENTER):
  58. self.close_timer()
  59. if self.hh==0 and self.mm==0 and self.ss==6:
  60. self.col=color.red
  61. if self.count==75:
  62. self.decrease_seconds()
  63. self.count=0
  64.  
  65.  
  66. def start(passer):
  67. root32=games.Screen(400, 200)
  68. sprite_image=games.load_image("img.bmp")
  69. back=games.load_image("box2.jpg")
  70. root32.set_background(back)
  71. x=Counter(root32, 296, 139, sprite_image, passer)
  72. root32.mainloop(50)

Tkinter timer(not working at all):
  1. #Gamal Crichton
  2. #Teje's Counter
  3. #04/02/07
  4. from Tkinter import *
  5. import time
  6. class Application(Frame):
  7. def __init__(self,master,act_name,time_):
  8. Frame.__init__(self,master)
  9. self.hh=time_[0]
  10. self.mm=time_[1]
  11. self.ss=time_[2]
  12. self.name=act_name
  13. self.grid()
  14. print time_
  15. self.disp_widgets()
  16. self.aggregate()
  17.  
  18.  
  19. def disp_widgets(self):
  20. #Uses labels to diaply time.
  21. Label(self,
  22. text=self.name, font=("Arial, 32")
  23. ).grid(row=0,column=0,columnspan=3,sticky=W)
  24.  
  25. self.hourlbl=Label(self,
  26. text=self.hh, font=("Arial, 40")
  27. )
  28. self.hourlbl.grid(row=1,column=0,columnspan=1,sticky=W)
  29. self.minutelbl=Label(self,
  30. text=self.mm, font=("Arial, 40")
  31. )
  32. self.minutelbl.grid(row=1,column=1,columnspan=1,sticky=W)
  33.  
  34.  
  35. self.secondlbl=Label(self,
  36. text=self.ss, font=("Arial, 40")
  37. )
  38. self.secondlbl.grid(row=1,column=2,columnspan=1,sticky=W)
  39.  
  40. Button (self,
  41. text="Stop",
  42. command=self.show,
  43. ).grid(row=2,column=0,sticky=E, columnspan=3)
  44.  
  45. def show(self):
  46. print "Booooooooo!"
  47. def aggregate(self): #This function is supposed to decrement the time
  48. for hours in range(self.hh, -1, -1):
  49. for minutes in range(self.mm, -1, -1):
  50. for seconds in range(self.ss, -1, -1):
  51. time.sleep(1)
  52. self.secondlbl["text"]=seconds
  53. if self.ss==0:
  54. self.ss=60
  55. self.minutelbl["text"]=minutes
  56. if self.mm==0:
  57. self.mm=0
  58. self.hourlbl["text"]=hours
  59. #Main
  60. root=Tk()
  61. root.title("Timer!")
  62. root.geometry("250x75")
  63. app= Application(root, "Timer", [2, 4, 5])
  64. root.mainloop()
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Tkinter Countdown Timer problems.

 
0
  #4
Feb 7th, 2007
Hey ... I recognize that version of livewires from "Python Programming for the Absolute Beginner", yes?

I've successfully done a timer before and recall that there was a subtle trick to it. I'll try to look it up at school.

In the meantime, I want to register a major complaint with the new formatting for [ code = Python ] and [/ code] tags. The line numbers make it impossible to cut-and-paste code from the posting into IDLE. Unless I'm missing something?

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Tkinter Countdown Timer problems.

 
0
  #5
Feb 8th, 2007
Jeff, yes you are missing something and so did I until Henri Bumsfeld pointed it out ...
http://www.daniweb.com/techtalkforums/post311639-5.html
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Tkinter Countdown Timer problems.

 
1
  #6
Feb 8th, 2007
Hi!

Change your aggregate function to:
  1. def aggregate(self): #This function is supposed to decrement the time
  2. for hours in range(self.hh, -1, -1):
  3. for minutes in range(self.mm, -1, -1):
  4. for seconds in range(self.ss, -1, -1):
  5. time.sleep(1)
  6. self.update() # <==
  7. ...

@jrcagle: Do you see the Toggle Plain Text below the code? Click it and be happy

Regards, mawe
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Tkinter Countdown Timer problems.

 
0
  #7
Feb 8th, 2007
Hey mawe. I take it I need to just write an update method with all the stuff that was there in it?

And yeah Jeff, it was that book. It's the one we use in school....well it's the one the school told us to take and go learn python on our own. It's sitting open on my printer to the livewires reference page right now, incidentally.

Well thanks guys for all your help. I'll try it when I get to work, cause I'm late for school right now. Let you know how it goes.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Tkinter Countdown Timer problems.

 
0
  #8
Feb 8th, 2007
Well that doesn't work either. The same thing happens: all i see is the console window.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Tkinter Countdown Timer problems.

 
1
  #9
Feb 8th, 2007
A couple of things wrong with your countdown timer.
1 - don't limit the size of your root window, widgets won't fit
2 - labels take strings
3 - root.update() is needed
Look this over:
[php]#Gamal Crichton
#Teje's Counter
#04/02/07

from Tkinter import *
import time

class Application(Frame):
def __init__(self,master,act_name,time_):
Frame.__init__(self,master)
self.hh=time_[0]
self.mm=time_[1]
self.ss=time_[2]
self.name=act_name
self.grid()
print time_
self.disp_widgets()
self.aggregate()


def disp_widgets(self):
#Uses labels to display time.
Label(self,
text=self.name, font=("Arial, 32")
).grid(row=0,column=0,columnspan=3,sticky=W)

self.hourlbl=Label(self,
text=str(self.hh), font=("Arial, 40") # str()!!!!!!!!!!
)
self.hourlbl.grid(row=1,column=0,columnspan=1,sticky=W)

self.minutelbl=Label(self,
text=str(self.mm), font=("Arial, 40") # str()!!!!!!!!!!
)
self.minutelbl.grid(row=1,column=1,columnspan=1,sticky=W)

self.secondlbl=Label(self,
text=str(self.ss), font=("Arial, 40") # str()!!!!!!!!!!
)
self.secondlbl.grid(row=1,column=2,columnspan=1,sticky=W)

Button (self,
text="Stop",
command=self.show,
).grid(row=2,column=0,columnspan=3,sticky=EW)

def show(self):
# needs more stuff
print "Booooooooo!"

def aggregate(self): #This function is supposed to decrement the time
for hours in range(self.hh, -1, -1):
for minutes in range(self.mm, -1, -1):
for seconds in range(self.ss, -1, -1):
time.sleep(1)
root.update() # needed!!!!!
self.secondlbl["text"]=str(seconds) # str()!!!!!!!!!!
if self.ss==0:
self.ss=60
self.minutelbl["text"]=str(minutes) # str()!!!!!!!!!!
if self.mm==0:
self.mm=0
self.hourlbl["text"]=str(hours) # str()!!!!!!!!!!

#Main
root=Tk()

root.title("Timer!")

#root.geometry("250x75") # do not use, too space restrictive!!!

app= Application(root, " Timer ", [2, 4, 5])

root.mainloop()
[/php]
Last edited by Ene Uran; Feb 8th, 2007 at 12:46 pm. Reason: spelling
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Tkinter Countdown Timer problems.

 
0
  #10
Feb 9th, 2007
WOOHOO! THanks you guys. I now have a working timer. The problem was that I mis-interpreted mawe's post and wrote my own self.update()....that didn't work. But it was from the last post that I got egged on that Tk objects have update methods, and I used it after
  1. self.hourlbl["text"]=self.hh
  2. #etc....
and it works just fine now.

Thanks!
Last edited by scru; Feb 9th, 2007 at 2:39 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC