[英]Python tkinter canvas going slow
这是Life Game的代码,我认为没有问题,并且可以正常工作。
但是,当我“启动”它时,它的绘制速度越来越慢。
我想知道为什么它的速度在变慢。
# -*- 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()
该代码创建多边形,但从不删除它们。
您需要明确删除它们。 使用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()
而不是使用全局变量,而是将程序构造为使用类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.