Creating fluid movement of an oval using tkinter

前端 未结 2 1741
盖世英雄少女心
盖世英雄少女心 2021-01-23 16:43

I\'m trying to create Connect-Four using tkinter. Once a disc is placed in a certain column, I want it to descend to the bottom of the column in a fluid movement.

I\'ve

相关标签:
2条回答
  • 2021-01-23 17:23

    You must pace the calls to move so that the movement is visible; canvas.after() allows you to call a function repeatedly, in this case until a condition is met (the disk arrived at destination)

    working code snippet

    import tkinter as tk
    
    
    def smooth_motion(counter):
         canvas.move(disc, 0, dy)
         counter -= 1
         if counter >= 0:
             canvas.after(10, smooth_motion, counter)
    
    root = tk.Tk()
    canvas = tk.Canvas(root, bg='cyan')
    canvas.pack()
    
    counter = 100
    disc = canvas.create_oval(200, 0, 210, 10, fill='green')
    dy = (100 - 0) / counter
    smooth_motion(counter)
    
    root.mainloop()
    
    0 讨论(0)
  • 2021-01-23 17:32

    You're missing function which shows changes to canvas - canvas.update(), try writing it after canvas.move().

    0 讨论(0)
提交回复
热议问题