6

我正在使用 Tkinter 编写康威的生活游戏,我想要一个“开始”按钮,它允许动画开始,并继续自动步进直到终止。我正在使用 Canvas 来绘制环境,但由于“Go”按钮需要在更新画布之前完成函数调用,所以窗口会一直挂起,直到我终止进程。我试图在我想更新画布的地方使用canvas.update_idletasks()and canvas.update()(然后睡几秒钟),但这似乎没有奏效。有任何想法吗?下面是我的 GameOfLife 类,Environment 类只管理单元的“板”。

from Tkinter import *
from random import *
from time import time
from Environment import *

class GameOfLife(object):
    def __init__(self, master, envDim):
        self.unitSize = 10
        self.dimension = envDim * self.unitSize
        self.environment = Environment(envDim)
        self.environment.seedBoard()
        self.started = False

        frame = Frame(master)
        frame.pack()
        Button(frame, text = "Go", command = self.go_call).pack(side = LEFT)
        Button(frame, text = "Clear", command = self.reset_call).pack(side = LEFT)
        Button(frame, text = "Close", command = frame.quit).pack(side = RIGHT)
        canvas = self.drawCanvas(master, self.dimension)

def drawCanvas(self, master, dimension):
    self.canvas = Canvas(master, width = self.dimension, height = self.dimension)
    self.canvas.pack()
    return self.canvas

def go_call(self):
    print "<< Go Call >>"
    if self.environment.started == False:
        self.environment.seedBoard()

    self.drawState(self.environment)
    self.environment.nextBoard()
    self.started = True
    while True:
        self.environment.nextBoard()
        self.canvas.delete(ALL)
        self.drawState(self.environment)
        self.canvas.update_idletasks()
        sleep(4)

def reset_call(self):
    print "<< Reset Call >>"
    self.canvas.delete(ALL)
    self.environment = Environment(self.environment.dim)

def drawState(self, environment):
    size = self.unitSize
    for x in range(environment.dim):
        for y in range(environment.dim):
            if environment.matrix[x][y].alive == True:
                xs = x * size
                ys = y * size
                self.canvas.create_rectangle(xs, ys, xs+size, ys+size, fill = 'black')

envDim = 70
root = Tk()
gol = GameOfLife(root, envDim)
root.mainloop()
4

1 回答 1

8

你不应该在你的 GUI 程序中放置一个无限循环,而且你绝对不应该调用 sleep。您已经运行了一个无限循环——事件循环——所以好好利用它。

制作这样的动画的方法是编写一个绘制动画一帧的函数(或游戏回合,选择你的比喻)。然后,让该函数通过after.

粗略地说,您的代码应如下所示:

def draw(self):
    # draw the board according to the current state
    ...
    # arrange for the next frame to draw in 4 seconds
    self.after(4000, self.draw)

def __init__(self, ...):
    ...
    self.go = tk.Button(self, text="Go", command=self.draw)
    ...

如果你想添加一个停止按钮,它需要做的就是设置一个标志。然后,在 中draw,只需在调用之前检查标志self.after

于 2012-06-09T23:57:07.597 回答