Are you ready for a challenge in Python programming? In this exercise, you will expand the already learned code from the last video. The focus is on not just drawing a circle on the Canvas, but also assigning a random color to the circle each time you click on it. Let’s go through the individual steps and find out how you can implement this.
Key Insights
- You will learn how to handle events in the GUI.
- You will understand how to use global variables.
- You will learn how to generate random numbers and use them for color selection.
Step-by-Step Instructions
Below are the steps necessary to implement the programming of the circle and the random color change.
Expand the Code: Import Necessary Libraries
Start by importing the required libraries. Since you are working with random numbers, make sure to import the random module to enable random generation.

Create the Canvas with the Circle
First, you should create a canvas where the circle will be drawn. Make sure to define the draw_circle function that draws the circle. You can set the starting color for the circle.

Click on the Circle: Insert Bind Function
Now it’s time to enable interaction. This is done through the bind method, which allows you to bind a click on the canvas to a function. Name the function that should be called when you click on the canvas.

Implement the Color Change Function
Now define the function was_clicked. Here you will implement the logic that will execute when clicking on the canvas. You need a global variable to store the current color. Initially, you set this to a default color, e.g., green.
def was_clicked(event):
global g_color
This is where the color changes
Generate Random Colors
Now use the random library to select a random color. In the was_clicked function, you can generate integers that correspond to different colors. If a number between 1 and 4 is drawn, you will set the corresponding color.

Display the New Color in the Console
To check the program flow, you want to see which color was clicked. At this point, you can output the new color in the console. Use the Print function to make this information visible.
Activate the Color Change in the Canvas
Now that the logic for the color change is in place, you must ensure that the draw_circle function is called to draw the circle with the new color. Make sure this function is called within was_clicked.

Check the Entire Program
Run your program and click on the circle in the canvas. You should observe how the color changes and how the new color is displayed in the console. Experiment to see if everything works as desired.

Summary – Python Programming for Beginners: Random Colors in the Canvas
In this guide, you learned how to introduce interactivity into your Python project by enabling the canvas to respond to clicks and randomly change the color of a drawn circle. You have learned fundamental techniques for working with global variables and the random library. With these skills, you can now further develop your Python programs and add custom functions.
Frequently Asked Questions
How can I expand functionality?You can add more shapes or increase the number of colors to choose from.
What are global variables?Global variables are variables defined outside of a function and available within functions.
How do I correctly import the random library?Use import random at the beginning of your code.
How can I make the circle bigger or smaller?Change the parameters of the draw_circle function to affect the size of the circle.