canvas 中的 tkinter 按钮功能未按正确顺序执行代码

tkinter buttons function in canvas doesnt executes code in right order

我目前正在编写略有改动的 Conway 人生游戏版本,但我遇到了 GUI 问题。

当我删除第 63 行 (buttonPressed(canvas,newIMG,arNew,x,y)) 时,一切正常并且 canvas 中的图像得到正确更新,但是如果我在 [=13] 的末尾添加这行代码=]函数,程序卡死了。

我知道它在没有结束的情况下回忆自己是一个无限循环,这不是问题,我的问题是为什么print("done")之后的代码会影响之前的代码,不应该只是执行吗一个接一个?

谢谢大家的帮助!!!

import numpy
import random
from tkinter import *
import time
import sys

def initWindow(x,y,ar):
    window = Tk()
    canvas = Canvas(window, width=x, height=y, bg="#ffffff")
    canvas.pack()
    img = PhotoImage(width=x,height=y)
    canvas.create_image((x/2, y/2), image=img, state="normal")
    button = Button(window, text="play", command=lambda:  buttonPressed(canvas, img, ar, x, y))
    button.configure(width= 10, activebackground= "#33B5E5", relief = FLAT)
    window = canvas.create_window(0,0,anchor=NW,window=button)
    iY = 0
    iX = 0
    while iX < x:
        while iY < y:

            if ar[iY][iX] == 1:
                img.put("#000000",(iX,iY))
            else:
                img.put("#ffffff", (iX, iY))

            iY = iY + 1
        iY = 0
        iX = iX + 1
    canvas.mainloop()


def buttonPressed(canvas,img,ar,x,y):
    arNew = ar
    newIMG = img
    ar = arNew
    arNew = playGame(ar,x,y)
    iY = 0
    iX = 0
    img = newIMG

    while iX < x:
        while iY < y:

            if arNew[iY][iX] == 1:
                newIMG.put("#000000", (iX, iY))
            else:
                newIMG.put("#ffffff", (iX, iY))

            iY = iY + 1
        iY = 0
        iX = iX + 1

    canvas.itemconfig(img, image=newIMG)
    canvas.pack()
    print("done")
    #here´s the problem
    buttonPressed(canvas,newIMG,arNew,x,y)


def createCoincidenceArray(x,y):
    ar = numpy.zeros((y, x))
    convert = False
    iY = 0
    iX = 0

    while iX < x:
        while iY < y:
            r = random.random()
            if r > 0.5:
                convert = True
            if convert:
                ar[iY][iX] = 1
            convert = False
            iY = iY + 1
        iY = 0
        iX = iX + 1

    return ar

def playGame(ar,x,y):
    iY = 0
    iX = 0
    arNew = ar
    #print(arNew)
    while iX < x:
        while iY < y:
            noN = numberOfNeighbours(ar,x,y,iX,iY)
            if noN > 2:
                arNew[iY][iX] = 0
            if noN == 2:
                arNew[iY][iX] = 1
            if noN < 2:
                arNew[iY][iX] = 0
            iY = iY + 1
        iY = 0
        iX = iX + 1
    ar = arNew
    return ar

def numberOfNeighbours(ar,x,y,iX,iY):
    nON = 0
    if iX != 0:
        nON = nON + ar[iY][iX - 1]
    if iX != 0 and iY != 0:
        nON = nON + ar[iY - 1][iX - 1]
    #oben
    if iY != 0:
        nON = nON + ar[iY - 1][iX]
    #oben rechts
    if iY != 0 and iX < x - 1:
        nON = nON + ar[iY - 1][iX + 1]
    #rechts
    if iX < x - 1:
        nON = nON + ar[iY][iX + 1]
    #rechts unten
    if iX < x - 1 and iY < y - 1:
        nON = nON + ar[iY + 1][iX + 1]
    #unten
    if iY < y - 1:
        nON = nON + ar[iY + 1][iX]
    #unten links
    if iY < y - 1 and iX != 0:
        nON = nON + ar[iY + 1][iX -1]

    return nON

x = 400
y = 200

ar = createCoincidenceArray(x,y)
arNew = playGame(ar,x,y)
initWindow(x,y,arNew)

解决方法:在canvas.pack()

后面加上canvas.update()即可

请随意使用代码 ;)