Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simpler way to draw a circle with tkinter?

Drawing a circle on a tkinter Canvas is usually done by the create_oval method. However, supplying the bounding box is often a confusing way to think about drawing a circle. It's not particularly difficult to come up with a shortcut for it, but I couldn't find anyone else doing something similar, so I'll post it in the hopes someone else finds it useful.

like image 451
mgold Avatar asked Aug 01 '13 04:08

mgold


People also ask

Is it possible to draw a circle directly in tkinter Canvas?

Drawing a circle on a tkinter Canvas is usually done by the create_oval method. However, supplying the bounding box is often a confusing way to think about drawing a circle.

Which function is used to draw a circle in a tkinter window?

Here the create_oval() method is used to create a circle item. The first four parameters are the bounding box coordinates of the circle. In other words, they are x and y coordinates of the top-left and bottom-right points of the box, in which the circle is drawn.

How do I make a circle in Python?

To draw a circle, we will use circle() method which takes radius as an argument. You must import turtle module in order to use it. Then, we have created a new drawing board and assigned it to an object t. It will draw a circle of radius 50units.


2 Answers

Here's a trick known as monkey patching where we actually add a member to the tkinter/Tkinter class Canvas. Below is a fully-functioning program (Python 2.7 and 3.x), of which the third paragraph is of interest. Add it to your code and you can treat tk.Canvas.create_circle(x, y, r, options...) as you would a builtin method, where the options are the same as create_oval. We do something similar for create_arc (fourth paragraph), and give the option to specify an end angle instead of an extent.

try:     import tkinter as tk except ImportError:     import Tkinter as tk  # Python 2  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 and "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.title("Circles and Arcs") root.mainloop() 

Result:

result of code

like image 98
mgold Avatar answered Sep 20 '22 03:09

mgold


simpler solution:

from tkinter import * root = Tk() myCanvas = Canvas(root) myCanvas.pack()  def create_circle(x, y, r, canvasName): #center coordinates, radius     x0 = x - r     y0 = y - r     x1 = x + r     y1 = y + r     return canvasName.create_oval(x0, y0, x1, y1)  create_circle(100, 100, 20, myCanvas) create_circle(50, 25, 10, myCanvas) root.mainloop() 
like image 21
Edgar Runnman Avatar answered Sep 17 '22 03:09

Edgar Runnman