Skip to content
Advertisement

How do I center an object on the canvas in Tkinter?

I want to center a circle on the canvas in Tkinter, but I want to combine whatever code does that with mine, without changing it up too much.

My program:

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=1000, height=1000)
canvas.pack()
canvas.create_arc(200, 200, 100, 100, extent=359, style=ARC)

Is that possible with what I am using currently?

Advertisement

Answer

You can just draw your circle in the middle of the canvas like this:

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=500, height=500)
canvas.pack()


radius = 50 #set the arc radius
canvas_middle = [int(canvas['width'])/2, int(canvas['height'])/2] #find the middle of the canvas
canvas.create_arc(canvas_middle[0] - radius, canvas_middle[1] - radius, canvas_middle[0] + radius, canvas_middle[1] + radius, extent=359, style=ARC)

tk.mainloop()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement