Computer >> Computer tutorials >  >> Programming >> Python

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 of the Canvas and supply it with a set of coordinates to draw the arc. We can use create_arc() to create an arc item, which can be a chord, a pieslice, or a simple arc.

Steps −

  • Import the required libraries and create an instance of tkinter frame.

  • Set the size of the frame using root.geometry method.

  • Create a Canvas widget and set its height and width. Also, set its background color with bg="blue".

  • Next, use the create_arc method to draw an arc. Supply the coordinates of the arc and also define the extent of the arc. Here, we have set extent=150.

  • Finally, run the mainloop of the application window.

Example

# Import the required libraries
from tkinter import *

# Create an instance of Tkinter Frame
root = Tk()

# Set the geometry
root.geometry("700x350")

# Create a Canvas with a background color
C = Canvas(root, bg="blue", height=250, width=600)

# Coordinates for the arc
coord = 100, 50, 500, 300

# Create the arc with extent=150
arc = C.create_arc(coord, start=0, extent=150, fill="red")

C.pack(side=TOP, padx=50, pady=50)

root.mainloop()

Output

On execution, it will produce the following output −

How to draw an arc on a tkinter canvas?