1

I'm currently working on a schoolproject in Python, I and some friends try to code "Connect Four" and we have to use tkinter as GUI.

My current goal is to animate the falling coins. I read some articles about tkinter .after() method, but I'm not sure how to implement this properly into a single function (It should just loop a few times until the coin is on its final position)

the time.sleep method is not nice (freezes the gui, no animation showing), i know that :(

Here a simplified Code >> i also had a idea to use the after method and let the called function return something, but i have no clue how to do this.

Would be nice if somebody give a hint how to get this working

 def addCoin(self, x):



    #gets called when the button is pressed, x is the column

    #seachest the highest(lowest on screen) free field >> the final field
    maxdown = 0

    #self.y >> fieldsize, maxdown is correct

    while maxdown < self.y and self.getValue(x, maxdown) == 0:
        maxdown +=1

    #to get the last empty field, not the first full
    maxdown -= 1
    print(str(maxdown)+" is the next empty field")

    if maxdown < 0:
        print("you cant place a coin here")
        return

    #animation, which is my problem
    animy = 0
    while animy < maxdown:
        #set field color to playercolor, wait , set field white,     next field
        self.setValue(x, animy, self.activeplayer.nr)
        time.sleep(0.15)
        self.setValue(x, animy, 0)
        animy += 1


    #After animation, check for win, works fine
  • Have you looked at the following example of animating an object? http://stackoverflow.com/a/25431690/7432 – Bryan Oakley Jan 05 '17 at 01:14
  • Jeah but it's not the same as it does not return at some point – Lokmeinmatz Jan 05 '17 at 11:49
  • 1
    You need to seperate out the animation from the addCoin function. By adding a coin you perhaps add an "add coin" event to the animation list. Then there is a function that is scheduled periodically using .after which draws the coin. – scotty3785 Jan 05 '17 at 15:09

0 Answers0