0

I am currently writing a slightly changed version of Conway's game of life and I am experiencing issues with the GUI.

When I delete line 63 (buttonPressed(canvas,newIMG,arNew,x,y)) everything works fine and the image in the canvas gets updated correctly, but if I add this line of code at the end of the buttonPressed function, the program is freezing.

I know it's a infinite loop when it's recalling itself without an ending, that's not the problem, my question is why is the code right after print("done") influencing the code before, shouldn't it just be executed in after an other?

Thank for your help guys!!!

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)
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • ok guys long story short i`ve found my problem, i forgot to ad a canvas.update(), now everything is working, feel free to use the code ;) – pythonCoder42 Jul 14 '18 at 22:42

1 Answers1

0

solution: just add canvas.update() after the canvas.pack()

feel free to use the code ;)

j_4321
  • 15,431
  • 3
  • 34
  • 61