2017-02-03 3 views
0

私が現在取り組んでいるPythonプログラムは、色を変えてそれを繰り返すメモリゲームです。この時点では終了していませんが、問題はクラスに書き込まれた時間関数です。これは、色の切り替え手順の後に起こりますが、色の切り替えが起こる前にまだ時間の遅延が起きています。したがって、私は遅延は、個々の色のスイッチの後に起こる、とだけ同じエラーがここPythonでコード化された順序の前に起こる時間の遅れ

time.sleep(2) 

で起こった後に日時を使用してみまし作る補正を必要とするコードは次のとおりです。time.sleep(...)を使用して

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

システムからキー/マウスイベントを取得し、ウィジェットに送信し、ウィジェットを更新し、ウィジェットを更新するなどの「mainloop」をブロックするため、 'sleep'と' while'を使用できません。遅延を伴う関数を実行するには 'after(milliseconds、function_name)'を使います。 – furas

答えて

0

はしていませんtkinterを使用しているときに動作します。遅れたい場合は、after()を使います。 afterコマンドを使用するには、tkモジュールを使用し、その後ろに時間と関数を置きます。次に例を示します。

root = Tk() 

def doSomething(): 
    something = 0 

root.after(1000, doSomething) 

時間はミリ秒単位です(例:1000ms = 1秒)。 私は助けてくれるといいと思います:)

関連する問題