2014-09-20 142 views
0

這是生活遊戲的代碼,我認爲沒有問題,它的工作原理。Python tkinter帆布變慢

但是,當我開始時,它的繪製速度變得越來越慢。

我想知道爲什麼它的速度正在放緩。

# -*- coding: cp949 -*- 
import Tkinter 
from time import sleep 

def check(): 
    """count alive neighbor cells and save it in list 'neighbor' 
    """ 
    global lifegame 
    global neighbor 
    global indexx 
    global indexy 
    for y in range(0, indexy): 
     for x in range(0, indexx): 
      neighbor[y][x] = 0 
    for y in range(0, indexy): 
     for x in range (0, indexx): 
      try: 
       if lifegame[y-1][x-1] == 1: 
        neighbor[y][x] += 1 
       if lifegame[y][x-1] == 1: 
        neighbor[y][x] += 1 
       if lifegame[y+1][x-1] == 1: 
        neighbor[y][x] += 1 
       if lifegame[y-1][x] == 1: 
        neighbor[y][x] += 1 
       if lifegame[y+1][x] == 1: 
        neighbor[y][x] += 1 
       if lifegame[y-1][x+1] == 1: 
        neighbor[y][x] += 1 
       if lifegame[y][x+1] == 1: 
        neighbor[y][x] += 1 
       if lifegame[y+1][x+1] == 1: 
        neighbor[y][x] += 1 
      except IndexError: 
       continue 

def change(): 
    """ 
    check neighbor num, and change lifegame 
    cell`s state 0 to 1 or 1 to 0 
    """ 
    global lifegame 
    global neighbor 
    global indexx 
    global indexy 
    for y in range(0, indexy): 
     for x in range(0, indexx): 
      if lifegame[y][x] == 0: 
       if neighbor[y][x] == 3: 
        lifegame[y][x] = 1 
       else: continue 
      elif lifegame[y][x] == 1: 
       if neighbor[y][x]<2 or neighbor[y][x]>3: 
        lifegame[y][x] = 0 
       else: continue 

def stop(): 
    """ 
    go is variable to stop while loop 
    """ 
    global go 
    go = False 

def start(): 
    """ 
    go is variable to stop while loop 
    """ 
    global go 
    go = True 
    checkgo() 

def checkgo(): 
    "when go is true, do while loop that will draw in canvas" 
    global go 
    while go: 
     check() 
     change() 
     draw() 
    print "end" 

def reset(): 
    """kill all cells and reset""" 
    global go 
    global lifegame 
    global neighbor 
    global indexx 
    global indexy 
    go = False 
    for y in range(0, indexy): 
     for x in range(0, indexx): 
      neighbor[y][x] = 0 
      lifegame[y][x] = 0 
    draw() 

def callback(event): 
    """mouse click event""" 
    global go 
    go = False 
    print "clicked at", event.x/10, event.y/10 
    a = event.x/10 
    b = event.y/10 
    global lifegame 
    global neighbor 
    if lifegame[b][a] == 0: 
     lifegame[b][a] = 1 
    elif lifegame[b][a] == 1: 
     lifegame[b][a] = 0 
    check() 
    draw() 

def draw(): 
    """draw cells on canvas""" 
    global lifegame 
    global indexx 
    global indexy 
    global tk 
    for y in range(0, indexy): 
     for x in range(0, indexx): 
      if lifegame[y][x] == 0: 
       canvas.create_polygon(10*x, 10*y, 10*x, 10+10*y, 10+10*x, 
             10+10*y, 10+10*x, 10*y, fill='white') 
      elif lifegame[y][x] == 1: 
       canvas.create_polygon(10*x, 10*y, 10*x, 10+10*y, 10+10*x, 
             10+10*y, 10+10*x, 10*y, fill='black') 
    tk.update() 

lifegame = [] 
neighbor = [] 
tk = Tkinter.Tk() 
indexx = 10 
indexy = 10 
go = False 
for a in range(0, indexy): 
    lifegame.append([0]*indexx) 
    neighbor.append([0]*indexx) 
menubar = Tkinter.Menu(tk) 
optionmenu = Tkinter.Menu(menubar, tearoff=0) 
optionmenu.add_command(label="Start", command=start) 
optionmenu.add_command(label="Stop", command=stop) 
optionmenu.add_command(label="Reset", command=reset) 
optionmenu.add_separator() 
optionmenu.add_command(label="Exit", command=tk.quit) 
tk.config(menu = menubar) 
menubar.add_cascade(label="Option", menu=optionmenu) 
canvas = Tkinter.Canvas(tk, width=300, height=300) 
canvas.pack() 
canvas.bind("<Button-1>", callback) 
draw()  
tk.mainloop() 

回答

0

該代碼創建多邊形,但從不刪除它們。

您需要明確刪除它們。使用Canvas.delete來做到這一點。

def draw(): 
    """draw cells on canvas""" 
    global lifegame 
    global indexx 
    global indexy 
    global tk 
    canvas.delete(Tkinter.ALL) # <---- Delete all the item created in the canvas. 
    for y in range(0, indexy): 
     for x in range(0, indexx): 
      if lifegame[y][x] == 0: 
       canvas.create_polygon(10*x, 10*y, 10*x, 10+10*y, 10+10*x, 
             10+10*y, 10+10*x, 10*y, fill='white') 
      elif lifegame[y][x] == 1: 
       canvas.create_polygon(10*x, 10*y, 10*x, 10+10*y, 10+10*x, 
             10+10*y, 10+10*x, 10*y, fill='black') 
    tk.update() 

而不是使用全局變量,結構程序使用類。

+0

謝謝我需要學習更多.... – 2014-09-20 09:27:43