画布中的tkinter按钮函数不执行右边的代码

2024-10-03 06:25:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在写一个略有改变的版本康威的生活游戏,我遇到的问题与图形用户界面

当我删除第63行(buttonPressed(canvas,newIMG,arNew,x,y))时,一切正常,画布中的图像得到正确更新,但是如果我在buttonPressed函数的末尾添加这行代码,程序就会冻结

我知道这是一个无限循环,当它无休止地回忆自己时,这不是问题所在,我的问题是,为什么在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)

Tags: 代码importimgifdefwindowcanvasar