用一种颜色在 Tkinter canvas 个对象之间填充 space
Fill space between Tkinter canvas objects with a colour
Python2.7.8,Windows7
我对 Tkinter canvas 小部件进行了子类化,并添加了一个新方法来创建圆边矩形。
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)
我想用一种颜色填充我创建的圆角矩形。我该怎么做。
与其绘制轮廓并尝试用红色填充内部,不如用预先存在的 canvas 对象构建圆角矩形?四个圆形馅饼片作为角,两个矩形形成一个十字?像这样:
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")
我还没有测试过这个,所以我可能弄错了坐标,或者输入了语法错误,但我想你会明白我的意思的。
我修改了 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 的代码,尽管它与我绘制的线条并不完全匹配。
原谅粗略的代码,我只是想在继续之前快速查看概念证明。
如果速度不重要,则可以计算线长。对于标准按钮和标签,可以预先定义它们以匹配固定半径。我有兴趣根据这些想法制作按钮和标签。
Python2.7.8,Windows7
我对 Tkinter canvas 小部件进行了子类化,并添加了一个新方法来创建圆边矩形。
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)
我想用一种颜色填充我创建的圆角矩形。我该怎么做。
与其绘制轮廓并尝试用红色填充内部,不如用预先存在的 canvas 对象构建圆角矩形?四个圆形馅饼片作为角,两个矩形形成一个十字?像这样:
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")
我还没有测试过这个,所以我可能弄错了坐标,或者输入了语法错误,但我想你会明白我的意思的。
我修改了 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 的代码,尽管它与我绘制的线条并不完全匹配。
原谅粗略的代码,我只是想在继续之前快速查看概念证明。
如果速度不重要,则可以计算线长。对于标准按钮和标签,可以预先定义它们以匹配固定半径。我有兴趣根据这些想法制作按钮和标签。