繁体   English   中英

是否可以在tkinter画布中填充弧形/椭圆形的外部?

[英]Is it possible to fill the outside of an arc/oval in tkinter canvas?

我正在尝试在tkinter画布上放置形状的图案。 到目前为止,我已经成功地用图案(如砌砖)填充了矩形,并且显示得很好:

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root)
canvas.config(borderwidth=0.0, highlightthickness=0.0)
bbox = (0, 0, 100, 100)
x1, y1, x2, y2 = bbox
width = 10
height = 5
def laying_bricks():
    for even, y in enumerate(range(y1, y2, height)):    
        x = 0
        while x < x2:
            increm = width //2 if even % 2 == 0 and x == 0 else width
            x_end = x + increm
            x_end = x_end if x_end < x2 else x2
            canvas.create_rectangle(x, y, x_end, y + height, fill='firebrick3', outline='khaki')
            x = x_end
laying_bricks()
# Add the final box outline for the shape
canvas.create_rectangle(*bbox)
canvas.pack()
root.mainloop()

对于锐角,我可以尝试在端部绘制单个多边形(不推荐),也可以将蒙版以一定角度应用于截止:

laying_bricks()
canvas.create_rectangle(*bbox)
triangulate = (0, 0, 0, 100, 100, 0)
canvas.create_polygon(triangulate, fill='black')

但是,如果我想在不规则形状(例如弧形/椭圆形)中填充相同的图案,那么在两端绘制单个图案不仅变得困难,而且不切实际。 可以为多边形截止形状创建自己的抛物线:

def create_parabola():
    points = [0, 0]
    for x in range(0, 200):
        y = 170 - sqrt(100 * x + 20)
        points.extend([x, y])
    points.extend([200, 0, 0, 0])
    canvas.create_polygon(points, fill='royalblue')
laying_bricks()
create_parabola()

但是话又说回来,这似乎是不切实际的,尤其是如果画布更大,并且它不能给我将曲线放置在有界区域内的精度。

因此,我的问题是:有没有一种明显的方法可以使用tkinter来填充弧/椭圆的“负空间”或“外部”区域? 或者,是否可以创建带有填充的反弧/椭圆形? 还是功能受到限制?

好吧,有两种不同的方法可以解决此问题。 对于弧,您所需要做的就是以您选择的颜色创建弧的其余部分。 例如,如果圆弧度为134度,则我们可以创建226度的圆弧,将圆/椭圆的完整360度相加。

extent = 134 # How far the arc goes
canvas.create_arc(100, 100, 200, 200, extent = extent) # Create first arc
canvas.create_arc(100, 100, 200, 200, extent = (360 - extent)) 
# Create second arc with same circle

对于椭圆形的边框,您有两种选择。 首先,您可以将画布背景设置为所需的颜色,如下所示:

canvas.create_oval(100, 100, 200, 200, width = 5000, outline = 'red') 
#The width of the border is 5000

这使得边框很大,以至于覆盖了整个屏幕。 如果要制作其他对象,请在制作初始椭圆形之后再制作。

您也可以使用bg操作将画布的背景设置为所需的颜色:

canvas = Canvas(master, width = 750, height = 500, bg = 'red') 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM