944,112 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 7098
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 7th, 2007
0

Tkinter Countdown Timer problems.

Expand Post »
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))
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Feb 7th, 2007
0

Re: Tkinter Countdown Timer problems.

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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 7th, 2007
0

Re: Tkinter Countdown Timer problems.

humblest apologies.


Livewires based timer:
python Syntax (Toggle Plain Text)
  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):
python Syntax (Toggle Plain Text)
  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()
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Feb 7th, 2007
0

Re: Tkinter Countdown Timer problems.

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 8th, 2007
0

Re: Tkinter Countdown Timer problems.

Jeff, yes you are missing something and so did I until Henri Bumsfeld pointed it out ...
http://www.daniweb.com/techtalkforums/post311639-5.html
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 8th, 2007
1

Re: Tkinter Countdown Timer problems.

Hi!

Change your aggregate function to:
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Feb 8th, 2007
0

Re: Tkinter Countdown Timer problems.

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.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Feb 8th, 2007
0

Re: Tkinter Countdown Timer problems.

Well that doesn't work either. The same thing happens: all i see is the console window.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Feb 8th, 2007
1

Re: Tkinter Countdown Timer problems.

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
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Feb 9th, 2007
0

Re: Tkinter Countdown Timer problems.

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
python Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Horizontal Buttom Placement
Next Thread in Python Forum Timeline: Tkinter Listbox Preselect





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC