python - Getting a function to execute periodically -
this question has answer here:
- how create timer using tkinter? 3 answers
i working on simple project , need little help. have created program draws circles on canvas. circles blue, , method flash() takes random int count, , light circle , change color yellow. problem want function continue execute every second or so, because want able let user of program click onto circle lit, , have program respond dialog stating correct/incorrect if user got right. of right can light 1 circle , it. tried using thread , set every 2 seconds, didn't work or maybe didn't correctly. appreciated.
from graphics import * tkinter import * import random class app(): def __init__(self): self.win = graphwin('demo2', 400, 300) # give title , dimensions count = random.randint(1,9) self.flash(count) def flash(self,count): circle1 = circle(point(50,30), 25) # set center , radius circle2 = circle(point(110,30), 25) circle3 = circle(point(170,30),25) circle4 = circle(point(50,90),25) circle5 = circle(point(110,90),25) circle6 = circle(point(170,90),25) circle7 = circle(point(50,150),25) circle8 = circle(point(110,150),25) circle9 = circle(point(170,150),25) circle1.setfill("blue") circle1.draw(self.win) circle2.setfill("blue") circle2.draw(self.win) circle3.setfill("blue") circle3.draw(self.win) circle4.setfill("blue") circle4.draw(self.win) circle5.setfill("blue") circle5.draw(self.win) circle6.setfill("blue") circle6.draw(self.win) circle7.setfill("blue") circle7.draw(self.win) circle8.setfill("blue") circle8.draw(self.win) circle9.setfill("blue") circle9.draw(self.win) if count==1: circle1.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 29 , mouseclick.y <= 31 , mouseclick.x >= 49 , mouseclick.x <= 51: print("correct") else: print("incorrect") elif count==2: circle2.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 29 , mouseclick.y <= 31 , mouseclick.x >= 109 , mouseclick.x <= 111: print("correct") else: print("incorrect") elif count==3: circle1.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 29 , mouseclick.y <= 31 , mouseclick.x >= 169 , mouseclick.x <= 171: print("correct") else: print("incorrect") elif count==4: circle4.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 89 , mouseclick.y <= 91 , mouseclick.x >= 49 , mouseclick.x <= 51: print("correct") else: print("incorrect") elif count==5: circle5.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 89 , mouseclick.y <= 91 , mouseclick.x >= 109 , mouseclick.x <= 111: print("correct") else: print("incorrect") elif count==6: circle6.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 89 , mouseclick.y <= 91 , mouseclick.x >= 169 , mouseclick.x <= 171: print("correct") else: print("incorrect") elif count==7: circle7.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 149 , mouseclick.y <= 151 , mouseclick.x >= 49 , mouseclick.x <= 51: print("correct") else: print("incorrect") elif count==8: circle8.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 149 , mouseclick.y <= 151 , mouseclick.x >= 109 , mouseclick.x <= 111: print("correct") else: print("incorrect") else: circle9.setfill("yellow") mouseclick = self.win.getmouse() if mouseclick.y >= 149 , mouseclick.y <= 151 , mouseclick.x >= 169 , mouseclick.x <= 171: print("correct") else: print("incorrect") if __name__ == "__main__": app = app() app.mainloop()
okay, may want try this: put in code somewhere in beginning import time
then, ever want delay, use this:time.sleep(number of seconds)
can include milliseconds in typing amount of seconds .1 or if want repeat every few seconds, this
while true: flash(self,count) #replace self , count whatever want time.sleep(x) #replace x amount of seconds, part optional though.
the while true:
makes loop while true == true
a.k.a. forever. if want loop amount of times, this
max=6 #6 amount of times want loop loop_count=1 # required later in loop while x <=max: #loops when x less or equal 6 flash(self,count) #replace self , count whatever want time.sleep(y) #this part if want delay @ all. loop_count+=1 #this makes every time loop runs, loop_count gains 1. # after runs first time, loop_count == 2 (which means starting second loop)
i hope made sense , help. if there errors please correct me
Comments
Post a Comment