-1

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()
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
  • I have already viewed that post and I have tried that solution to no avail. Have you tried to run my code so you can see what I am talking about? – user3571085 Apr 25 '14 at 02:56
  • Have you read the answers too? You've mentioned only that you've tried a thread based solution. I can't run your code because I don't have the `graphics` module. – Cristian Ciupitu Apr 25 '14 at 06:50
  • The [Code a timer in a python GUI in Tkinter](http://stackoverflow.com/q/2400262/7432) question has the right solution. You say you tried it to no avail, but the code in your question doesn't show that you tried it. – Bryan Oakley Apr 25 '14 at 11:11
  • Bryan Oakley I appreciate the suggestion, but I did indeed try that. It was actually the first solution that I tried. Have you tried running my program using this method and did you get it to work? – user3571085 Apr 25 '14 at 17:54

1 Answers1

-2

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

mdlp0716
  • 190
  • 1
  • 8
  • Where exactly do you suggest I put the while loop? I tried a could of different places and it doesn't get the job done, so I don't know if there is something wrong with the way it is coded or what. Have you tried to run my code and seen what I talking about? Everything runs fine, I just need it to CONTINUE to loop so it will keep lighting up random circles – user3571085 Apr 25 '14 at 02:15
  • 4
    **This is bad advice**. Never put a sleep in GUI code because it will do just that -- make your program sleep. That means it won't respond to events, it won't refresh the screen, it will be frozen. – Bryan Oakley Apr 25 '14 at 11:10
  • oh srry for the bad advice im not really good with python im not exactly sure how to use graphics and such, so keep looking for advice – mdlp0716 Apr 26 '14 at 16:26