I am working on a simple project and I need a little help. I have created a program that draws circles on a canvas. The circles are blue, and the method flash() takes a random int count, and will light up a circle and change its color to yellow. The problem is I want the function to continue to execute every second or so, because I want to eventually be able to let the user of the program click onto the circle that is lit, and have the program respond with dialog stating Correct/Incorrect if the user got it right. As of right now I can only get it to light one circle and that is it. I tried using a thread and set it to every 2 seconds, but that didn't work or maybe I didn't do something correctly. Any help would be appreciated.
from graphics import *
import tkinter as tk
import threading
import random
class App():
def __init__(self):
self.win = GraphWin('Demo2', 400, 300) # give title and dimensions
count = random.randint(1,9)
self.flash(count)
t = threading.Timer(2.0,flash)
t.start()
def flash(self,count):
circle1 = Circle(Point(50,30), 25) # set center and 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 and mouseClick.y <= 31 and mouseClick.x >= 49 and mouseClick.x <= 51:
print("Correct")
else:
print("Incorrect")
elif count==2:
circle2.setFill("yellow")
mouseClick = self.win.getMouse()
if mouseClick.y >= 29 and mouseClick.y <= 31 and mouseClick.x >= 109 and mouseClick.x <= 111:
print("Correct")
else:
print("Incorrect")
elif count==3:
circle1.setFill("yellow")
mouseClick = self.win.getMouse()
if mouseClick.y >= 29 and mouseClick.y <= 31 and mouseClick.x >= 169 and mouseClick.x <= 171:
print("Correct")
else:
print("Incorrect")
elif count==4:
circle4.setFill("yellow")
mouseClick = self.win.getMouse()
if mouseClick.y >= 89 and mouseClick.y <= 91 and mouseClick.x >= 49 and mouseClick.x <= 51:
print("Correct")
else:
print("Incorrect")
elif count==5:
circle5.setFill("yellow")
mouseClick = self.win.getMouse()
if mouseClick.y >= 89 and mouseClick.y <= 91 and mouseClick.x >= 109 and mouseClick.x <= 111:
print("Correct")
else:
print("Incorrect")
elif count==6:
circle6.setFill("yellow")
mouseClick = self.win.getMouse()
if mouseClick.y >= 89 and mouseClick.y <= 91 and mouseClick.x >= 169 and mouseClick.x <= 171:
print("Correct")
else:
print("Incorrect")
elif count==7:
circle7.setFill("yellow")
mouseClick = self.win.getMouse()
if mouseClick.y >= 149 and mouseClick.y <= 151 and mouseClick.x >= 49 and mouseClick.x <= 51:
print("Correct")
else:
print("Incorrect")
elif count==8:
circle8.setFill("yellow")
mouseClick = self.win.getMouse()
if mouseClick.y >= 149 and mouseClick.y <= 151 and mouseClick.x >= 109 and mouseClick.x <= 111:
print("Correct")
else:
print("Incorrect")
else:
circle9.setFill("yellow")
mouseClick = self.win.getMouse()
if mouseClick.y >= 149 and mouseClick.y <= 151 and mouseClick.x >= 169 and mouseClick.x <= 171:
print("Correct")
else:
print("Incorrect")
if __name__ == "__main__":
app = App()
app.mainloop()