python

时间:2017-02-03 12:02:47

标签: python class time

我目前正在研究的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()

1 个答案:

答案 0 :(得分:0)

使用tkinter时,使用time.sleep(...)不起作用。如果您想延迟,请使用after()。要使用after命令,请使用tk模块并在其后面放置时间和功能。这是一个例子:

root = Tk()

def doSomething():
    something = 0

root.after(1000, doSomething)

时间以毫秒为单位,例如1000ms = 1秒。 希望我能帮忙:)。