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:
1 |
import tkinter as tk |
If you’re using Python 2.x, import Tkinter as follows:
1 |
import Tkinter as tk |
Step 2: Create the main window
Next, create the main window for your application using the Tk class:
1 2 |
window = tk.Tk() window.title("Circle Drawing") |
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:
1 2 |
canvas = tk.Canvas(window, width=400, height=400, bg="white") canvas.pack() |
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():
1 2 3 4 5 6 7 8 9 10 11 12 |
x_center = 200 y_center = 200 radius = 50 # Calculate the coordinates of the bounding box x1 = x_center - radius y1 = y_center - radius x2 = x_center + radius y2 = y_center + radius # Draw the circle circle = canvas.create_oval(x1, y1, x2, y2, fill="red") |
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:
1 |
window.mainloop() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import tkinter as tk # Create the main window window = tk.Tk() window.title("Circle Drawing") # Create a Canvas widget canvas = tk.Canvas(window, width=400, height=400, bg="white") canvas.pack() # Draw a circle on the Canvas x_center = 200 y_center = 200 radius = 50 # Calculate the coordinates of the bounding box x1 = x_center - radius y1 = y_center - radius x2 = x_center + radius y2 = y_center + radius # Draw the circle circle = canvas.create_oval(x1, y1, x2, y2, fill="red") # Start the main event loop window.mainloop() |
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!