Creating Fluid Movement Of An Oval Using Tkinter
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 tried using the move command of the Canvas class but I'm unsure if I am using it incorrectly or perhaps I am better off deleting and re-drawing the oval each iteration. Currently, the disc indeed moves but not in a fluid way. It simply draws it in the new location.
This is the disc moving function:
counter = 0 self.__canvas.create_oval(100,200,0,100, fill='yellow') self.__canvas.create_oval(100,300,0,200, fill='yellow') self.__canvas.create_oval(100,400,0,300, fill='brown') disc = self.__canvas.create_oval(200,400,100,300, fill='green') # trying to move this specific disc while counter < 10: self.__canvas.move(disc, 0, counter) counter += 1
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()
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module