Question

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 *
from tkinter import *
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)

    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()
Was it helpful?

Solution

okay, so you may want to try this: put this in your code somewhere in the beginning import time then, where ever you want a delay, you use this:time.sleep(number of seconds) you can include milliseconds in it by typing the amount of seconds as .1 or something if you want it to repeat every few seconds, you do this

while True:
    flash(self,count) #replace self and count with whatever you want
    time.sleep(x) #replace x with amount of seconds, this part is optional though.

the while True: makes it loop while True == True a.k.a. forever. if you only want it to loop a certain amount of times, do something like this

max=6 #6 is the amount of times you want it to loop
loop_count=1 # this is required for later in the loop
while x <=max: #loops ONLY when x is less than or equal to 6
    flash(self,count) #replace self and count with whatever you want
    time.sleep(y) #this part is only if you want it to delay at all.
    loop_count+=1 #this makes it so that every time the loop runs, loop_count gains 1. 
    # after it runs the first time, loop_count == 2 (which means it is starting its second loop)

i hope this made sense to you and that i could help. if there are any errors please correct me

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top