在 Tkinter (Python) 中画圆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17985216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:40:26  来源:igfitidea点击:

Draw circle in Tkinter (Python)

pythontkintergeometry

提问by mgold

Drawing a circle on a tkinter Canvasis usually done by the create_ovalmethod. 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.

在 a 上画圆tkinter Canvas通常是通过create_oval方法来完成的。然而,提供边界框通常是考虑绘制圆的一种令人困惑的方式。想出一个捷径并不是特别困难,但我找不到其他人做类似的事情,所以我会发布它,希望其他人觉得它有用。

采纳答案by mgold

Here's a trick known as "monkey patching" where we actually add a member to the Tkinterclass Canvas. Below is a fully-functioning program (Python 2.7), 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 endangle instead of an extent.

这是一个称为“猴子补丁”的技巧,我们实际上向Tkinter类中添加了一个成员Canvas。下面是一个功能齐全的程序(Python 2.7),其中第三段很有趣。将它添加到您的代码中,您可以tk.Canvas.create_circle(x, y, r, options...)像对待内置方法一样对待它,其中的选项与create_oval. 我们对create_arc(第四段)做了类似的事情,并提供指定end角度而不是extent.

import Tkinter as 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 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.wm_title("Circles and Arcs")
root.mainloop()

Result:

结果:

result of code

代码结果

回答by Edgar Runnman

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()