Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to draw an arc on a tkinter canvas?
The Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text, widgets or frames on a Canvas.
To draw an arc on a tkinter Canvas, we will use the create_arc() method. This method creates an arc item which can be displayed as a chord, pieslice, or simple arc based on the parameters provided.
Syntax
canvas.create_arc(x1, y1, x2, y2, **options)
Parameters
The create_arc() method accepts the following key parameters ?
x1, y1, x2, y2 − Coordinates defining the bounding rectangle
start − Starting angle in degrees (default is 0)
extent − Angular extent of the arc in degrees
style − Arc style: "arc", "chord", or "pieslice"
fill − Fill color for the arc
outline − Outline color of the arc
Example
Here's how to create a simple arc on a tkinter canvas ?
# Import the required libraries
from tkinter import *
# Create an instance of Tkinter Frame
root = Tk()
root.title("Arc Drawing Example")
# Set the geometry
root.geometry("700x350")
# Create a Canvas with a background color
canvas = Canvas(root, bg="lightblue", height=250, width=600)
# Coordinates for the arc (bounding rectangle)
x1, y1, x2, y2 = 100, 50, 500, 300
# Create the arc with extent=150 degrees
arc = canvas.create_arc(x1, y1, x2, y2, start=0, extent=150, fill="red", outline="black", width=2)
canvas.pack(side=TOP, padx=50, pady=50)
root.mainloop()
Different Arc Styles
You can create different types of arcs using the style parameter ?
from tkinter import *
root = Tk()
root.title("Different Arc Styles")
root.geometry("800x400")
canvas = Canvas(root, bg="white", height=350, width=750)
# Simple arc
canvas.create_arc(50, 50, 200, 150, start=0, extent=180, style="arc", outline="blue", width=3)
canvas.create_text(125, 170, text="Arc Style", fill="blue")
# Chord arc
canvas.create_arc(250, 50, 400, 150, start=0, extent=180, style="chord", fill="green", outline="darkgreen")
canvas.create_text(325, 170, text="Chord Style", fill="green")
# Pieslice arc (default)
canvas.create_arc(450, 50, 600, 150, start=0, extent=180, style="pieslice", fill="orange", outline="red")
canvas.create_text(525, 170, text="Pieslice Style", fill="orange")
canvas.pack(pady=20)
root.mainloop()
Creating Multiple Arcs
You can create multiple arcs with different angles and colors ?
from tkinter import *
root = Tk()
root.title("Multiple Arcs")
root.geometry("600x500")
canvas = Canvas(root, bg="white", height=450, width=550)
# Center coordinates and radius
center_x, center_y = 275, 225
radius = 150
# Create multiple arcs with different colors and extents
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
start_angle = 0
for i, color in enumerate(colors):
extent = 60 # Each arc covers 60 degrees
canvas.create_arc(center_x - radius, center_y - radius,
center_x + radius, center_y + radius,
start=start_angle, extent=extent,
fill=color, outline="black", width=2)
start_angle += extent
canvas.pack(pady=25)
root.mainloop()
Conclusion
The create_arc() method provides flexible options for drawing arcs on tkinter Canvas. Use different styles like "arc", "chord", or "pieslice" to create various visual effects, and combine multiple arcs to create complex shapes and patterns.
