Draw circle in Tkinter (Python) -


drawing circle on tkinter canvas done create_oval method. however, supplying bounding box confusing way think drawing circle. it's not particularly difficult come shortcut it, couldn't find else doing similar, i'll post in hopes else finds useful.

here's trick known "monkey patching" add member tkinter class canvas. below fully-functioning program (python 2.7), of third paragraph of interest. add code , can treat tk.canvas.create_circle(x, y, r, options...) builtin method, options same create_oval. similar create_arc (fourth paragraph), , give option specify end angle instead of extent.

import tkinter tk  root = tk.tk() canvas = tk.canvas(root, width=200, height=200, borderwidth=0, highlightthickness=0, bg="black") canvas.grid()  def _create_circle(self, x, y, r, **kwargs):     return self.create_oval(x-r, y-r, x+r, y+r, **kwargs) tk.canvas.create_circle = _create_circle  def _create_circle_arc(self, x, y, r, **kwargs):     if "start" in kwargs , "end" in kwargs:         kwargs["extent"] = kwargs["end"] - kwargs["start"]         del kwargs["end"]     return self.create_arc(x-r, y-r, x+r, y+r, **kwargs) tk.canvas.create_circle_arc = _create_circle_arc  canvas.create_circle(100, 120, 50, fill="blue", outline="#ddd", width=4) canvas.create_circle_arc(100, 120, 48, fill="green", outline="", start=45, end=140) canvas.create_circle_arc(100, 120, 48, fill="green", outline="", start=275, end=305) canvas.create_circle_arc(100, 120, 45, style="arc", outline="white", width=6, start=270-25, end=270+25) canvas.create_circle(150, 40, 20, fill="#bbb", outline="")  root.wm_title("circles , arcs") root.mainloop() 

result:

result of code


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -