Tkinter (I believe it is simply pronounced as “Tee-Kay-Inter” at least, that’s how I say it) is a module in python that allows for a graphical canvas that can be altered and “drawn’ on. I created a python file that animates to circles to move in a straight line until they hit an edge.
from tkinter import *
import random
import time
#assign the module to a var
tk = Tk()
#set global vars
Height = 500
Width = 500
canvas = Canvas(tk, width = Width, height = Height)
tk.title("Graphics")
canvas.pack()
color1 = "red"
color2 = "blue2"
ball = canvas.create_oval(0, 0, 100, 100, fill = color1)
gball = canvas.create_oval(0, 0, 100, 100, fill = color2)
xspeed = 10
yspeed = 0
gxspeed = 0
gyspeed = 10
while True:
canvas.move(ball, xspeed, yspeed)
canvas.move(gball, gxspeed, gyspeed)
pos = canvas.coords(ball) #[left, top, right, bottom]
gpos = canvas.coords(gball)
if pos[2]>=Width:
xspeed = -xspeed
if pos[0]<=0:
xspeed = -xspeed
if gpos[1]==0:
gyspeed = -gyspeed
if gpos[3]==Height:
gyspeed = -gyspeed
tk.update()
time.sleep(0.1)
Here is the result:

So, to analyze this code, we should break it down by sections.
“from tkinter import *
import random
import time”
Importing simply means to go to that module, in this case “tkinter,” and be ready to use it later.
“#assign the module to a var
tk = Tk()
#set global vars
Height = 500
Width = 500
canvas = Canvas(tk, width = Width, height = Height)
tk.title(“Graphics”)
canvas.pack()”
Comments in Python are done via hashtags – these lines won’t be read by the computer and produce an error. In this chunk of code, We set the basic parameters (width, height, etc.) for the canvas that will display our amazing work of art.
“color1 = “red”
color2 = “blue2″
ball = canvas.create_oval(0, 0, 100, 100, fill = color1)
gball = canvas.create_oval(0, 0, 100, 100, fill = color2)
xspeed = 10
yspeed = 0
gxspeed = 0
gyspeed = 10”
Here we set our variables that directly affect the circles/balls. The variables “ball” and “gball” refer to the two balls and here is a perfect reason of why we use variables: gball had originally meant “green ball” to distinguish between balls, however “color1″ and color2” are allowed to be quickly changed whenever so I changed it to blue and it did not affect the implementation of the code.
the “(0,0,100,100)” referred to the position and shape of the circles in the window.
The x and y speeds of each ball refer to their horizontal and vertical speeds, respectively.
“while True:
canvas.move(ball, xspeed, yspeed)
canvas.move(gball, gxspeed, gyspeed)
pos = canvas.coords(ball) #[left, top, right, bottom]
gpos = canvas.coords(gball)”
“while True:” is a loop statement that will use the code until the loop is broken. Since I did not want the circles to stop bouncing, I never made it so it did not equal True.
“canvas.move” is pretty self-explanatory: on the canvas, move (ball name, horizontal speed, vertical speed).
I created pos and gpos to record the current position of the balls so that I could tell it to bounce back when it reached a wall. It saves it in this format: [left, top, right, bottom] so pos[0] is the left position and then it counts up for each section (ex: pos[3] is the bottom position)
” if pos[2]>=Width:
xspeed = -xspeed
if pos[0]<=0:
xspeed = -xspeed
if gpos[1]==0:
gyspeed = -gyspeed
if gpos[3]==Height:
gyspeed = -gyspeed”
IF statements only execute when a requirement is met. So if the position of index 2 ( the right side because Python counts from 0+) is greater than or equal to Width (see why we made it a variable at the beginning? We can simply use “Width” instead of saying 500 pixels, which could change later)
Once it hits the side, it changes its horizontal speed to the negative equivalent ( so if it goes 10..speed, then it changes to -10) This causes it to reverse. Then the less than or equal to of pos[0] means that the circle reverses again when it hits the left side, thus bouncing back and forth.
Then I repeat this with gball and gpos, except that this ball bounces up and down, so the pos[] is changed as well as the edge variables.
“tk.update()
time.sleep(0.1)”
Finally, this snippet just updates the frame every x amount of time so that we can see clear results.
This project was really interesting and very key to helping me learn basic syntax and functions. Yet I want to do something bigger, maybe something that is more interactive. I found 3 links that may have interesting projects that I intend to check out and attempt soon!
https://codeclubprojects.org/en-GB/python/