Reputation: 326
I want to create this shape in python tkinter:
Yet the only options I have to use are
canvas.create_polygon
canvas.create_line
canvas.create_arc
canvas.create_oval
None of these options above have the capability to generate such shape. Are there any alternatives or ways I can create this shape using these options?
Upvotes: 0
Views: 6055
Reputation: 23443
I think this is what you're looking for.
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()
coord = 10, 50, 240, 210
arc = canvas.create_arc(coord, start=30, extent=120, style=tk.ARC, width=3)
root.mainloop()
Upvotes: 0
Reputation: 41862
As @martineau comments, create_arc()
is the method but understanding tkinter's create_oval()
is crucial as an arc is a slice of an oval:
import tkinter as tk
WINDOW_WIDTH, WINDOW_HEIGHT = 600, 300
OVAL_WIDTH, OVAL_HEIGHT = 576, 290
# (x0, y0, x1, y1) rectangle for oval
BOUNDS = ( \
(WINDOW_WIDTH - OVAL_WIDTH) / 2, \
(WINDOW_HEIGHT - OVAL_HEIGHT) / 2, \
3*WINDOW_WIDTH/2 - OVAL_WIDTH/2, \
3*WINDOW_HEIGHT/2 - OVAL_HEIGHT/2 \
)
root = tk.Tk()
canvas = tk.Canvas(root, width=WINDOW_WIDTH+20, height=WINDOW_HEIGHT+20) # +20 for window "chrome"
canvas.pack()
rectangle = canvas.create_rectangle(*BOUNDS, outline="blue") # just for illustration
oval = canvas.create_oval(*BOUNDS, outline="red") # just for illustration
arc = canvas.create_arc(*BOUNDS, start=30, extent=120, style=tk.ARC, width=3)
root.after(3000, canvas.delete, rectangle) # remove rectangle illustration
root.after(6000, canvas.delete, oval) # remove oval illustration
root.mainloop()
When I have used Arc, it creates a line at the bottom as an outline to the arc - how can I get rid of this?
The style=tk.ARC
above takes care of this -- the default is a pie slice.
How the ends of this arc terminate aren't identical to your illustration. As far as I can tell, tkinter's capstyle
and joinstyle
options aren't available to arcs.
Upvotes: 2
Reputation: 385830
The create_arc
method is what you want to use. It creates an arc in one of three different styles, specified by the style
parameter. This is how the official tcl/tk documentation describes the style
option:
If type is pieslice (the default) then the arc's region is defined by a section of the oval's perimeter plus two line segments, one between the center of the oval and each end of the perimeter section. If type is chord then the arc's region is defined by a section of the oval's perimeter plus a single line segment connecting the two end points of the perimeter section. If type is arc then the arc's region consists of a section of the perimeter alone. In this last case the fill option is ignored.
Here's an example of the three styles:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, background="black")
canvas.pack(side="top", fill="both", expand=True)
canvas.create_arc(0, 20, 100, 120, outline="red", style="pieslice")
canvas.create_arc(80, 20, 180, 120, outline="red", style="chord")
canvas.create_arc(160, 20, 260, 120, outline="red", style="arc")
root.mainloop()
Upvotes: 2