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)
我想用单一颜色填充我创建的圆角矩形.我该如何解决这个问题.
最佳答案 不是绘制轮廓并尝试用红色填充内部,为什么不用预先存在的画布对象构建圆角矩形?角落有四个圆形饼状切片,两个矩形形成十字形?像这样的东西:
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")
我没有测试过这个,所以我可能错误地找到了坐标,或者输入了语法错误,但我想你会明白我的意思.