使用 Python 的 tkinter 的 Canvas 绘图时,如何使绘制的图形抗锯齿?
加上这个参数 "smooth=True"
canvas.create_line(x1, y1, x2, y2, smooth=True)
可以使用 Canvas 的 smooth 属性来抗锯齿,具体方法如下:
# 创建 Canvas 对象
canvas = tkinter.Canvas(root, width=400, height=400, bg='white')
# 设置 smooth 属性为 True
canvas.config(smooth=True)
# 绘制图形
canvas.create_oval(10, 10, 390, 390, outline='red', width=2)
不知道你这个问题是否已经解决, 如果还没有解决的话:from tkinter import *
import math as m
class App:
def __init__(self, master):
# self.Canvas1(master)
self.CanvasP(master)
def Canvas1(self, master):
# 创建Canvas
w = Canvas(master, width=200, height=100, bg='#fff')
w.pack()
# 创建图形
line1 = w.create_line(0, 50, 200, 50, fill='#22F633')
line2 = w.create_line(100, 0, 100, 100, fill='red', dash=(4, 4))
rect1 = w.create_rectangle(50, 25, 150, 75, fill='blue')
# 移动图形
w.coords(line1, 0, 25, 200, 25)
w.itemconfig(rect1, fill='red')
w.delete(line2)
# 椭圆
w.create_oval(50, 25, 150, 75, fill='pink')
# 创建多边形
cx, cy, r = 100, 50, 40
r1, r2 = int(r * m.sin(2 * m.pi / 5)), int(r * m.cos(2 * m.pi / 5))
r3, r4 = int(r * m.sin(m.pi / 5)), int(r * m.cos(m.pi / 5))
points = [
cx - r1, cy - r2,
cx + r1, cy - r2,
cx - r3, cy + r4,
cx, cy - r,
cx + r3, cy + r4,
]
w.create_polygon(points, outline='green', fill='yellow')
w.create_oval(cx - r, cy - r, cx + r, cy + r, outline='blue', width=2)
# 文字
w.create_text(100, 50, text='Harry')
# 按钮
Button(master, text='删除全部', command=lambda x=ALL: w.delete(x)).pack()
def CanvasP(self, master):
f1, f2 = Frame(master), Frame(master)
f1.pack(side=TOP, fill=X, padx=5, pady=5)
f2.pack(side=BOTTOM, fill=X, padx=5, pady=5)
w = Canvas(f1, width=400, height=200, bg='#fff')
w.pack()
def paint(event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
w.create_oval(x1, y1, x2, y2, fill='red')
w.bind('<B1-Motion>', paint)
Label(f2, text='写下你的留言:...').pack(side=LEFT)
Button(f2, text='清空', command=lambda x=ALL: w.delete(x)).pack(side=RIGHT)
if __name__ == '__main__':
root = Tk()
root.title('Tk-Canvas')
app = App(root)
root.mainloop()
![]() | ![]() |
---|