Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading