Cha*_*too 5 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)
Run Code Online (Sandbox Code Playgroud)
我想用单一颜色填充我创建的圆角矩形。我该怎么办呢。
与其绘制轮廓并尝试用红色填充内部,为什么不使用预先存在的画布对象构建圆角矩形呢?四个圆形饼片作为角,两个矩形形成一个十字?像这样的东西:
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")
Run Code Online (Sandbox Code Playgroud)
我还没有测试过这个,所以我可能得到了错误的坐标,或者输入了语法错误,但我想你会明白我的意思。