How To Draw A Circle In Python Tkinter

In this tutorial, we will learn how to draw a circle using Python’s Tkinter library. Tkinter is a built-in library in Python that can be used to create graphical user interfaces (GUI).

We’ll be focusing on creating a simple application that will demonstrate how to draw a circle on a Tkinter canvas widget.

Step 1: Import the Tkinter module

First, you need to import the Tkinter module into your Python script. If you’re using Python 3.x, import it as follows:

If you’re using Python 2.x, import Tkinter as follows:

Step 2: Create the main window

Next, create the main window for your application using the Tk class:

This will create a new application window with the title “Circle Drawing”.

Step 3: Create a Canvas widget

To draw a circle, we will use a Canvas widget. A Canvas widget is a flexible widget that can be used to draw various shapes, including circles, lines, and rectangles.

Create a new Canvas widget with a specific width and height:

The bg parameter sets the background color of the canvas to white.

Step 4: Draw a circle on the Canvas

To draw a circle on the canvas, we will use the create_oval() method of the Canvas widget. This method creates an oval shape based on the bounding box you provide.

Here’s an example of how to draw a circle using create_oval():

In this example, we have specified the center of the circle as (200, 200) with a radius of 50. Then, we calculated the coordinates of the bounding box and used the create_oval() method to draw the circle on the canvas.

Step 5: Start the main event loop

Finally, start the main event loop by calling the mainloop() method of the window:

This will keep the application running until the user closes the window.

Full Code

Here’s the complete code to draw a circle on a Tkinter canvas:

Output

Here’s the output of the code:

Conclusion

In this tutorial, we demonstrated how to draw a circle on a Tkinter canvas using the create_oval() method.

Tkinter is a powerful and flexible library for creating graphical user interfaces in Python, and this example just scratches the surface of what you can do with it.

Keep exploring the Tkinter documentation and examples to create more advanced GUI applications!