画布中的tkinter按钮功能无法按正确的顺序执行代码

时间:2018-07-14 22:31:50

标签: python tkinter tkinter-canvas

我目前正在编写康维生活游戏的稍有变化的版本,并且遇到图形用户界面的问题。

当我删除第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)

1 个答案:

答案 0 :(得分:0)

解决方案:只需在canvas.update()之后添加canvas.pack()

随意使用代码;)