获取定期执行的功能

时间:2014-04-25 01:11:40

标签: python tkinter

我正在开展一个简单的项目,我需要一些帮助。我创建了一个在画布上绘制圆圈的程序。圆圈为蓝色,方法flash()采用随机int计数,并点亮一个圆圈并将其颜色更改为黄色。问题是我希望函数每隔一秒左右继续执行一次,因为我希望最终能够让程序的用户点击被点亮的圆圈,并让程序响应对话框说明正确/不正确用户说得对。截至目前,我只能点亮一个圆圈,就是这样。我尝试使用一个线程并将其设置为每2秒,但这没有用,或者我没有做正确的事情。任何帮助,将不胜感激。

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()

1 个答案:

答案 0 :(得分:-2)

好的,所以你可能想试试这个: 把它放在代码的某个地方import time 然后,无论你想要什么延迟,你都可以使用:time.sleep(number of seconds) 您可以通过键入秒数为.1或其他内容来包含毫秒数 如果你想让它每隔几秒重复一遍,你就这样做了

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.

while True:使True == True a.k.a.永远循环。如果您只想让它循环一定次数,请执行类似这样的操作

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)

我希望这对你有意义,我可以提供帮助。如果有任何错误请更正我