Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

animations on canvas tkinter

import tkinter
import time
 
# width of the animation window
animation_window_width=800
# height of the animation window
animation_window_height=600
# initial x position of the ball
animation_ball_start_xpos = 50
# initial y position of the ball
animation_ball_start_ypos = 50
# radius of the ball
animation_ball_radius = 30
# the pixel movement of ball for each iteration
animation_ball_min_movement = 5
# delay between successive frames in seconds
animation_refresh_seconds = 0.01
 
# The main window of the animation
def create_animation_window():
  window = tkinter.Tk()
  window.title("Tkinter Animation Demo")
  # Uses python 3.6+ string interpolation
  window.geometry(f'{animation_window_width}x{animation_window_height}')
  return window
 
# Create a canvas for animation and add it to main window
def create_animation_canvas(window):
  canvas = tkinter.Canvas(window)
  canvas.configure(bg="black")
  canvas.pack(fill="both", expand=True)
  return canvas
 
# Create and animate ball in an infinite loop
def animate_ball(window, canvas,xinc,yinc):
  ball = canvas.create_oval(animation_ball_start_xpos-animation_ball_radius,
            animation_ball_start_ypos-animation_ball_radius,
            animation_ball_start_xpos+animation_ball_radius,
            animation_ball_start_ypos+animation_ball_radius,
            fill="blue", outline="white", width=4)
  while True:
    canvas.move(ball,xinc,yinc)
    window.update()
    time.sleep(animation_refresh_seconds)
    ball_pos = canvas.coords(ball)
    # unpack array to variables
    xl,yl,xr,yr = ball_pos
    if xl < abs(xinc) or xr > animation_window_width-abs(xinc):
      xinc = -xinc
    if yl < abs(yinc) or yr > animation_window_height-abs(yinc):
      yinc = -yinc
 
# The actual execution starts here
animation_window = create_animation_window()
animation_canvas = create_animation_canvas(animation_window)
animate_ball(animation_window,animation_canvas, animation_ball_min_movement, animation_ball_min_movement)
Comment

PREVIOUS NEXT
Code Example
Python :: distance matrix gogle map python 
Python :: 2d array in python 
Python :: how to download from url in python 
Python :: python dropbox 
Python :: flask sending post request 
Python :: python fibonacci function 
Python :: convert 2d aray into 1d using python 
Python :: how to delete previous message using discord.py 
Python :: django model get field verbose name 
Python :: uninstall python3 from source on centos 7 
Python :: change the side of the axis plt python 
Python :: add list to end of list python 
Python :: python: convert variable as character 
Python :: sqlalchemy convert row to dict 
Python :: bitwise and python image 
Python :: how to filter a series in pandas 
Python :: python remove one element from numpy array 
Python :: start python server 
Python :: tf.reduce_sum() 
Python :: decode utf8 whit python 
Python :: how does HTTPServer work in python 
Python :: pandas write csv 
Python :: how to check django version 
Python :: restrict ticks to integers matplotlib 
Python :: convert pandas group to dict 
Python :: python logistic function 
Python :: python bisect 
Python :: dataframe of one row 
Python :: check if an object has an attribute in Python 
Python :: pandas rolling mean 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =