我使用create_rectangle在无限循环中绘制和更新画布上的WxH网格。我注意到几分钟后刷新速度大大减慢。经过阅读,我意识到create_rectangle创建了一个对象,我认为这是导致性能问题的原因。有没有办法在不创建新矩形的情况下改变矩形对象的颜色?如果没有,是否需要在重新绘制网格之前删除网格中的所有矩形?我认为性能问题是由于创造了成千上万的长方形,永远不会消失。
# Conway's Game of Life
# Algorithm and code taken from Al Sweigart's "Automate the Boring Stuff with Python"
import random, time, copy
from tkinter import *
# import gc
ASCII = False
WIDTH = 50
HEIGHT = 50
side = 15 # Length of a cell's side
def genesis():
for x in range(WIDTH):
for y in range(HEIGHT):
if random.randint(0, 1) == 1:
nextCells[x][y] = True
draw_world(nextCells)
def draw_world(grid):
for x in range(WIDTH):
for y in range(HEIGHT):
if grid[x][y]:
if ASCII:
print('#', end='')
else:
canvas.create_rectangle(x*side, y*side, x*side+side, y*side+side, fill='black')
else:
if ASCII:
print(' ', end='') # Print the # or space.
else:
canvas.create_rectangle(x*side, y*side, x*side+side, y*side+side, fill='white', outline="white") # no grid lines
if ASCII:
print() # Print a newline at the end of the row.
def live():
# root.update()
currentCells = copy.deepcopy(nextCells)
# Calculate the next step's cells based on current step's cells:
for x in range(WIDTH):
for y in range(HEIGHT):
# Get neighboring coordinates:
# `% WIDTH` ensures leftCoord is always between 0 and WIDTH - 1
leftCoord = (x - 1) % WIDTH
rightCoord = (x + 1) % WIDTH
aboveCoord = (y - 1) % HEIGHT
belowCoord = (y + 1) % HEIGHT
# Count number of living neighbors:
numNeighbors = 0
if currentCells[leftCoord][aboveCoord]:
numNeighbors += 1 # Top-left neighbor is alive.
if currentCells[x][aboveCoord]:
numNeighbors += 1 # Top neighbor is alive.
if currentCells[rightCoord][aboveCoord]:
numNeighbors += 1 # Top-right neighbor is alive.
if currentCells[leftCoord][y]:
numNeighbors += 1 # Left neighbor is alive.
if currentCells[rightCoord][y]:
numNeighbors += 1 # Right neighbor is alive.
if currentCells[leftCoord][belowCoord]:
numNeighbors += 1 # Bottom-left neighbor is alive.
if currentCells[x][belowCoord]:
numNeighbors += 1 # Bottom neighbor is alive.
if currentCells[rightCoord][belowCoord]:
numNeighbors += 1 # Bottom-right neighbor is alive.
# Set cell based on Conway's Game of Life rules:
if currentCells[x][y] and (numNeighbors == 2 or numNeighbors == 3):
# Living cells with 2 or 3 neighbors stay alive:
nextCells[x][y] = True
elif not currentCells[x][y] and numNeighbors == 3:
# Dead cells with 3 neighbors become alive:
nextCells[x][y] = True
else:
# Everything else dies or stays dead:
nextCells[x][y] = False
draw_world(nextCells)
# del currentCells
if ASCII:
time.sleep(1) # 1-second pause between frames
# gc.collect()
root.after(1000, live)
root = Tk()
root.title("Conway Game of Life")
frame = Frame(root, width=WIDTH*side, height=HEIGHT*side)
frame.pack(expand=True, fill=BOTH)
canvas = Canvas(frame, bg='white', width=WIDTH*side, height=HEIGHT*side)
canvas.pack(side=LEFT, expand=True, fill=BOTH)
nextCells = [[False] * WIDTH for i in range(HEIGHT)]
genesis()
live()
root.mainloop()发布于 2022-09-02 18:40:43
是否有一种方法可以改变矩形对象的颜色而不创建新的矩形?
是。该方法称为itemconfigure。您需要矩形id或与矩形关联的标记的名称。
canvas = tk.Canvas(...)
rectangle = canvas.create_rectangle(...)
...
canvas.itemconfigure(rectangle, fill="red")https://stackoverflow.com/questions/73586402
复制相似问题