Steps to achieve the Perfect Way to move forward learning More Gadgets : Any Kid Can Code
By: Laxman Singh
Tags: Python3, Kids And Tech, Python, Kids, Python Programming

In the last blog, we understood GUI programming and little bit of introduction to tkinterface. I hope you will be enjoying practicing and making different shapes in few lines of code. This time we will learn bit more about gadgets and try to make different figures do something which will be more interesting and challenging to all of us. To refresh the previous knowledge gained, you can take a look to below:

We are gradually moving to more and more programming. You would have noticed as we are moving ahead lines of code is becoming modular and reduced, and output is getting more better and better. Modular is buzzword and if you are able to understand how to reduce the code, make the maintainability easy, giving useful names to variables and there is no repetition of code in your program. Then, let me tell you are doing much more better than 10+ years of experienced so called programmers in today’s industry.

Let us move to understand some of the gadgets and our playground canvas

Canvas : tkinter is an interface to us. And, for that interface we have one area where we will make our widgets. This is Canvas. It starts from 0,0 coordinates and expand upto the given height and width at the time of creation

Time to know bit of gadgets and how they are turning up in our canvas:

Oval : Oval here, represents both circle and oval. How it turns up! First of all, for creating any widgets we have to use canvas and then inside canvas we have all the shapes available. Canvas is none other than a function or an object in class tkinter.

So, command to create oval:

canvas.create_oval(x0,x1,y1,y0)

And we will have oval or circle turn up in our canvas. Confusing! why to pass 4 parameters to create circle. So far, our mathematics says, x0, y0 and the radius is enough to create a circle. Here, there is a understanding, it makes a square or rectangle with the 4 parameters and then make the circle/oval inside it. You guessed it correct, whenever it is square then the shape will turn up in circle.

Rectangle: Rectangle or square means same here. If you give same value for coordinates, it will make square otherwise rectangle. Command to do so:

canvas.create_rectange(x0,y0,x1,y1)

Polygon: Polygon allows to give more parameters and it will help to make triangle and other shapes. I will let you play with this and make different figures

Let us make a rectangle and make it move inside our canvas, Below is the program

'''

Program to make rectangle and make it move in canvas

'''

# importing important libraries - we already know about all of them

from tkinter import *

import random

import time



# defining colors so that everytime you run this program will have different color rectangle

color = ["red", "green", "black", "blue", "yellow"]



# creating an instance of tkinterface

tk = Tk()



# deciding height and width of the canvas, we will pass this while creating canvas below

# WIDTH and HEIGHT are constant to give your program more understanding

WIDTH = 500

HEIGHT = 500



# creating our playground

canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg=random.choice(color))

canvas.pack()





# creating widget i.e, rectangle here and assigning it to variable

rectangle = canvas.create_rectangle(10,20,60,60,fill=random.choice(color))



# deciding the speed of the rectangle to move

# again both are variables and positive value will make it move forward and vice versa

moveXCoord = 1

moveYCoord = 3



# function to keep track of position

def checkBoundary(shape, moveXCoord, moveYCoord):

'''

This function will check the current position

in case figures is moving out of canvas

it will change the movement to negative

and (negative negative makes positive)

So, it will work iteratively

:return: new position to move

negative means move backward

positive means move forward

'''

pos = canvas.coords(shape)

if pos[3] >= HEIGHT or pos[1] <= 0:

moveYCoord = -moveYCoord

if pos[2] >= WIDTH or pos[0] <= 0:

moveXCoord = -moveXCoord

return moveXCoord, moveYCoord



# Introducing infinite loop

# it will run the program till you wont close it

while True:

canvas.move(rectangle, moveXCoord, moveYCoord)

moveXCoord, moveYCoord = checkBoundary(rectangle, moveXCoord, moveYCoord)



tk.update()

time.sleep(0.001)



tk.mainloop()

output of the program, i have take the still but it will be moving

And, see we moved to the next level where we are able to make any gadget in the canvas and move it in our playground. Task for you:

Practice and make more than 1 gadget in this canvas and move it. Make a loop and pick the coordinates randomly to turn up with different figures in the canvas. Then, try to make them move.

I hope you will have great time in doing that. Next, will be more advanced and deep dive into that. Let us get expertise in this. And, I will be back soon!