使用颜色填充Tkinter画布对象之间的空间

时间:2015-07-27 07:47:33

标签: python tkinter tkinter-canvas

Python 2.7.8,Windows 7

我已经将Tkinter画布小部件子类化了,我添加了一个新方法来创建一个带有圆边的矩形。

import Tkinter as tk

class MyCanvas(tk.Canvas):
    def __init__(self, *args, **kwargs):
        tk.Canvas.__init__(self, *args, **kwargs)

    def create_rounded(self, x1, y1, x2, y2, r):
        self.create_arc(x1, y1, x1+r, y1+r, start=90, extent=90, style=tk.ARC)
        self.create_arc(x2-r, y1, x2, y1+r, start=0, extent=90, style=tk.ARC)
        self.create_arc(x1, y2-r, x1+r, y2, start=180, extent=90, style=tk.ARC)
        self.create_arc(x2-r, y2-r, x2, y2, start=270, extent=90, style=tk.ARC)
        self.create_line(x1+r/2, y1, x2-r/2, y1)
        self.create_line(x1, y1+r/2, x1, y2-r/2)
        self.create_line(x1+r/2, y2, x2-r/2, y2)
        self.create_line(x2, y1+r/2, x2, y2-r/2)

我想用单一颜色填充我创建的圆角矩形。我该怎么做呢。

This is what I get when I run with some parameters

This is what I want

2 个答案:

答案 0 :(得分:2)

为什么不用预先存在的画布对象构建圆角矩形,而不是绘制轮廓并尝试用红色填充内部?角落有四个圆形饼状切片,两个矩形形成十字形?像这样:

import Tkinter as tk

class MyCanvas(tk.Canvas):
    def __init__(self, *args, **kwargs):
        tk.Canvas.__init__(self, *args, **kwargs)

    def create_rounded(self, x1, y1, x2, y2, r):
        self.create_arc(x1, y1, x1+r, y1+r, start=90, extent=90, style=tk.PIESLICE, fill = "red")
        self.create_arc(x2-r, y1, x2, y1+r, start=0, extent=90, style=tk.PIESLICE, fill = "red")
        self.create_arc(x1, y2-r, x1+r, y2, start=180, extent=90, style=tk.PIESLICE, fill = "red")
        self.create_arc(x2-r, y2-r, x2, y2, start=270, extent=90, style=tk.PIESLICE, fill = "red")
        self.create_rectangle(x1+r/2, y1-r/2, x2-r/2, y2+r/2, fill = "red")
        self.create_rectangle(x1, y1, x2, y2, fill="red")

我还没有对此进行过测试,所以我可能错误地设置了坐标,或者输入了语法错误,但我认为你会明白我的意思。

答案 1 :(得分:0)

我修改了saulspatz的建议来修正坐标,如下所示。不幸的是,它为弧线和矩形绘制线条。我添加了width = 0但是由于某种原因仍然看到一些行。一种选择可能是用填充颜色的线条绘制这些线条。看起来有点单调乏味。

class Rounded():
def __init__(self, canvas, x1, y1, x2, y2, r, color='red'):
    self.canvas = canvas  
    self.canvas.create_arc(x1, y1, x1+r, y1+r, start=90, extent=90, style=TK.PIESLICE, fill = color, width=0)
    self.canvas.create_arc(x2-r, y1, x2, y1+r, start=0, extent=90, style=TK.PIESLICE, fill = color, width=0)
    self.canvas.create_arc(x1, y2-r, x1+r, y2, start=180, extent=90, style=TK.PIESLICE, fill = color, width=0)
    self.canvas.create_arc(x2-r, y2-r, x2, y2, start=270, extent=90, style=TK.PIESLICE, fill = color, width=0)
    self.canvas.create_rectangle(x1+r/2, y1, x2-r/2, y2, fill=color, width=0)
    self.canvas.create_rectangle(x1, y1+r/2, x2, y2-r/2, fill=color, width=0)

然后我考虑画水平线。这里的优点是您可以实现对象的渐变。代码在这里: -

    def using_lines(self):
    # Try to draw rounded rectangle using lines!
    xx = [3,5,6,7,8,8,9,9,9,10]
    zz = [10,9,9,9,8,8,7,6,5,3]
    x = 10
    y = 150
    width = 100
    height = 30
    radius = 10
    #self.canvas.create_rectangle(x,y,x+width, y+height)
    r_width = width - 2*radius
    left = x + radius
    right = x + width - radius
    for r in range(radius):
        self.canvas.create_line(left, y+r, right, y+r, fill='green')
        left = x + radius - xx[r] 
        right = x + width - radius + xx[r]
    for r in range(height - 2 * r):
        self.canvas.create_line(x, y+radius+r, x + width, y+radius+r, fill='green')
    left = x
    right = x + width
    top = y + height - radius
    for r in range(radius):
        self.canvas.create_line(left, top+r, right, top+r, fill='green')
        left = x + (radius - zz[r])
        right = x + width - radius + zz[r]

我猜测数字可以绘制不同长度的线以匹配弧线,但我确信可以设计出计算结果。

这些线条都可以被赋予相同的标签,因此更改整个颜色很容易。为了添加边框,我还添加了Charlito的代码,尽管它与我绘制的线条并不完全匹配。

原谅原始代码,我只想在进一步研究之前快速查看概念证明。

如果速度不重要,则可以计算线长。对于标准按钮和标签,可以预先定义它们以匹配固定半径。我有兴趣用这些想法制作按钮和标签。