Tkinter and Small Projects

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:

ezgif.com-video-to-gif


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/

https://knightlab.northwestern.edu/2014/06/05/five-mini-programming-projects-for-the-python-beginner/

http://www.programmingforbeginnersbook.com/blog/what_should_i_make_beginner_programming_project_ideas/

 

How I Am Starting

I am using mainly online resources and tutorial to teach me python. Both Codecademy and Learnpython are two websites that I have found useful. I have been mostly using Learnpython and I have progressed through most of the basic lessons.

One reason I want to learn python is to help my sister who is taking a CS class. The class is focused on python and by my learning, I want to help her with her code. I have helped her with some of her projects, or labs, but I still am lacking knowledge in many areas. I think the combination of Learnpython and Codecademy will help me learn various skills regarding python.

The Change To Python

My passion is just about anything with technology and computers; my original intention was to learn the programming language Swift and then develop an app for CSF. Attempting to learn the language led to problem after problem; Apple has made it next to impossible to learn the language without their hardware (a Mac, which can go upwards of $1,000).

After maybe a week of experimentation, I was able to get around this by creating a “simulation” of a Mac (a virtual machine) but that it was less than optimal because the only way I could create a virtual Mac was by downloading the .iso, like downloading Windows 10, but every image was bootleg and outdated. Up to date software is the only way to use Xcode because Apple made it a requirement for whatever reason. With this, I am unable to learn Swift and thus unable to develop apps, however, to continue with my passion for tech and programming, I am amending my project to focus on learning Python.

Python is another programming language, but it is able to be learned and implemented on Windows systems. Python is versatile and is widely implemented all around – from power Intel hardware to creating and powering Instagram servers. Python is also a large focus of Computer Science classes and majors in college, so my learning of Python will also help me in college, since I want to major in Computer Science, and will also help me in any tech-related field.