python中编码顺序之前发生的时间延迟

python中编码顺序之前发生的时间延迟,python,class,time,Python,Class,Time,我目前正在开发的Python程序是一个记忆游戏,可以改变颜色,然后重复。在这一点上,它还没有完成,但我的问题是写入类中的时间函数。它发生在颜色切换程序之后,但在颜色切换发生之前,时间延迟仍在发生。因此,我需要一个修正,使延迟发生在每个单独的颜色切换之后,并且只有在同一个错误发生后才尝试使用datetime time.sleep(2) 代码如下: from tkinter import * from random import * from tkinter import font from t

我目前正在开发的Python程序是一个记忆游戏,可以改变颜色,然后重复。在这一点上,它还没有完成,但我的问题是写入类中的时间函数。它发生在颜色切换程序之后,但在颜色切换发生之前,时间延迟仍在发生。因此,我需要一个修正,使延迟发生在每个单独的颜色切换之后,并且只有在同一个错误发生后才尝试使用datetime

time.sleep(2) 
代码如下:

from tkinter import *
from random import *
from tkinter import font
from time import*
class Window(object):
    def __init__(self,master):
        self.bluecolour = "#103"
        self.lightblue = "#109"
        self.greencolour = "#130"
        self.lightgreen = "#172"
        self.redcolour = "#701"
        self.lightred = "#806"
        self.yellowcolour = "#987"
        self.lightyellow = "#992"
        self.master = master
        self.master.title("Simon Says")
        self.frame = Frame(self.master)
        self.top_frame = Frame(self.frame)
        self.bottom_frame = Frame(self.frame)
        self.blue_square = Button(self.master,bg = self.bluecolour, command = self.colourchangeblue, width = 10, height = 10)
        self.green_square = Button(self.master,bg = self.greencolour, command = self.colourchangegreen, width = 10, height = 10)
        self.red_square = Button(self.master,bg = self.redcolour, command = self.colourchangered, width = 10, height = 10)
        self.yellow_square = Button(self.master,bg = self.yellowcolour, command = self.colourchangeyellow, width = 10, height = 10)
        self.startbutton = Button(self.master,text = "Start Game",bg = "black",fg = "white",command = self.begin)


        self.frame.pack()
        self.top_frame.pack(fill = "x")
        self.bottom_frame.pack(fill = "x")
        self.blue_square.pack(in_=self.top_frame, side="left")
        self.red_square.pack(in_=self.top_frame, side="left")
        self.green_square.pack(in_=self.bottom_frame,side = "left")
        self.yellow_square.pack(in_=self.bottom_frame,side = "left")
        self.startbutton.pack(fill = "x")



    def wait_minute(self):
        import time # This is required to include time module
        import datetime
        time = datetime.datetime.now().strftime("%S")
        time = int(time)
        print("time")
        wait = time + 2
        if wait >= 60:
            wait -= 60
        while time != wait:
            time = datetime.datetime.now().strftime("%S")
            time = int(time)
        return 0



    def colourchangeblue(self):
        colour = self.blue_square['bg']
        if colour == self.bluecolour:
            self.blue_square.configure(bg=self.lightblue)
        else:
            self.blue_square.configure(bg=self.bluecolour)
        print("Colour blue change")



    def colourchangegreen(self):
        colour = self.green_square['bg']
        if colour == self.greencolour:
            self.green_square.configure(bg=self.lightgreen)
        else:
            self.green_square.configure(bg=self.greencolour)
        print("Colour green change")




    def colourchangered(self):
        colour = self.red_square['bg']
        if colour == self.redcolour:
            self.red_square.configure(bg=self.lightred)
        else:
            self.red_square.configure(bg=self.redcolour)
        print("Colour red change")
    def colourchangeyellow(self):
        colour = self.yellow_square['bg']
        if colour == self.yellowcolour:
            self.yellow_square.configure(bg=self.lightyellow)
        else:
            self.yellow_square.configure(bg=self.yellowcolour)
        print("Colour yellow change")



    def colour_switches(self):
        for x in self.runs:
            if x == 1:
                print("1")
                self.colourchangeblue()
            if x == 2:
                print("2")
                self.colourchangegreen()
            if x == 3:
                print("3")
                self.colourchangered()
            if x == 4:
                print("4")
                self.colourchangeyellow()
            if True:
                print(self.wait_minute())


    def game(self): 
        for x in range (1,self.rounds):
            self.runs.append(randint(1,4))
        print(self.runs)
        self.rounds += 1
        self.colour_switches()
    def begin(self):
        self.runs = []
        self.rounds = 3
        self.game()


def main(): 
    root = Tk()
    app = Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()
使用tkinter时,使用
time.sleep(…)
不起作用。如果要延迟,请使用after()。要使用
after
命令,请使用tk模块并将时间和函数放在其后面。下面是一个例子:

root = Tk()

def doSomething():
    something = 0

root.after(1000, doSomething)
时间以毫秒为单位,例如,1000ms=1秒。
希望我能提供帮助:)

您不能使用
睡眠
while
,因为它会阻止
mainloop
,mainloop从系统获取按键/鼠标事件,将它们发送到小部件,更新小部件,重新绘制小部件等。使用
在(毫秒,函数名)
之后延迟执行函数。