如何在 tkinter 中平滑椭圆周围的边缘?
How to smooth the edges around an oval in tkinter?
我是 tkinter 的新手,我最近了解了 canvas 小部件。在掌握了基础知识之后,我认为我可以使用 canvas 制作一个 自定义进度条 并且成功了!这真的很棒,然而,在删除形状上的轮廓后,我看到形状的边缘非常粗糙,尤其是椭圆形。我做了一些搜索,发现您只能对多边形使用 smooth 关键字参数。所以我的问题是有没有一种方法可以平滑形状的边缘,尤其是 tkinter 中的椭圆形,有点像 抗锯齿 ?我的代码:
from tkinter import *
root = Tk()
root.geometry("500x500")
percent = 0
progress_done, new_oval = None, None
canvas = Canvas(root, width=400, height=400, bg="white")
canvas.pack(pady=10)
prg_bar = canvas.create_rectangle(50, 140, 350, 160, fill="grey", outline="")
oval_one = canvas.create_oval(40, 140, 60, 160, fill="grey", outline="", )
oval_two = canvas.create_oval(340, 140, 360, 160, fill="grey", outline="")
def add_up():
global percent, progress_done, new_oval
if percent < 100:
percent += 20
if progress_done is None and new_oval is None:
canvas.create_oval(40, 140, 60, 160, fill="light blue", outline='')
canvas.delete(oval_one)
progress_done = canvas.create_rectangle(50, 140, (percent * 3) + 50, 160, fill="light blue", outline="")
new_oval = canvas.create_oval((percent * 3) + 40, 140, (percent * 3) + 60, 160, fill="light blue",
outline="")
else:
canvas.delete(progress_done)
canvas.delete(new_oval)
progress_done = canvas.create_rectangle(50, 140, (percent * 3) + 50, 160, fill="light blue", outline="")
new_oval = canvas.create_oval((percent * 3) + 40, 140, (percent * 3) + 60, 160, fill="light blue",
outline="")
if percent == 100:
canvas.delete(oval_two)
btn = Button(root, text="Add to prg", command=add_up)
btn.pack()
root.mainloop()
用 create_polygon
画出你的椭圆。下面是绘制椭圆和圆形矩形的示例。这两个示例都设置了 splinesteps
选项以增强生成曲线的平滑度。 splinesteps
选项默认为 12。
import tkinter as tk
root = tk.Tk()
c = tk.Canvas(root)
c.pack(fill='both', expand=True, anchor='nw')
def poly_oval(x, y, width, height, resolution=32):
points = [x, y,
x+width, y,
x+width, y+height,
x, y+height,
x, y]
return c.create_polygon(points, fill='#f00', smooth=True, splinesteps=resolution)
def poly_roundrect(x, y, width, height, radius, resolution=32):
#this is not a true round rect
#it's a round rect with the potential to have invisible garbage that cheats
#it cheats convincingly if you don't use an outline when it is cheating
radius = min(min(width, height), radius*2)
points = [x, y,
x+radius, y,
x+(width-radius), y,
x+width, y,
x+width, y+radius,
x+width, y+(height-radius),
x+width, y+height,
x+(width-radius), y+height,
x+radius, y+height,
x, y+height,
x, y+(height-radius),
x, y+radius,
x, y]
rect = c.create_polygon(points, fill='#1f1', smooth=True, splinesteps=resolution)
#display vertices
#for i in range(0, len(points), 2):
# poly_oval(points[i]-2, points[i+1]-2, 4, 4)
return rect
rect = poly_roundrect(10, 10, 300, 50, 10, 64)
root.mainloop()
我是 tkinter 的新手,我最近了解了 canvas 小部件。在掌握了基础知识之后,我认为我可以使用 canvas 制作一个 自定义进度条 并且成功了!这真的很棒,然而,在删除形状上的轮廓后,我看到形状的边缘非常粗糙,尤其是椭圆形。我做了一些搜索,发现您只能对多边形使用 smooth 关键字参数。所以我的问题是有没有一种方法可以平滑形状的边缘,尤其是 tkinter 中的椭圆形,有点像 抗锯齿 ?我的代码:
from tkinter import *
root = Tk()
root.geometry("500x500")
percent = 0
progress_done, new_oval = None, None
canvas = Canvas(root, width=400, height=400, bg="white")
canvas.pack(pady=10)
prg_bar = canvas.create_rectangle(50, 140, 350, 160, fill="grey", outline="")
oval_one = canvas.create_oval(40, 140, 60, 160, fill="grey", outline="", )
oval_two = canvas.create_oval(340, 140, 360, 160, fill="grey", outline="")
def add_up():
global percent, progress_done, new_oval
if percent < 100:
percent += 20
if progress_done is None and new_oval is None:
canvas.create_oval(40, 140, 60, 160, fill="light blue", outline='')
canvas.delete(oval_one)
progress_done = canvas.create_rectangle(50, 140, (percent * 3) + 50, 160, fill="light blue", outline="")
new_oval = canvas.create_oval((percent * 3) + 40, 140, (percent * 3) + 60, 160, fill="light blue",
outline="")
else:
canvas.delete(progress_done)
canvas.delete(new_oval)
progress_done = canvas.create_rectangle(50, 140, (percent * 3) + 50, 160, fill="light blue", outline="")
new_oval = canvas.create_oval((percent * 3) + 40, 140, (percent * 3) + 60, 160, fill="light blue",
outline="")
if percent == 100:
canvas.delete(oval_two)
btn = Button(root, text="Add to prg", command=add_up)
btn.pack()
root.mainloop()
用 create_polygon
画出你的椭圆。下面是绘制椭圆和圆形矩形的示例。这两个示例都设置了 splinesteps
选项以增强生成曲线的平滑度。 splinesteps
选项默认为 12。
import tkinter as tk
root = tk.Tk()
c = tk.Canvas(root)
c.pack(fill='both', expand=True, anchor='nw')
def poly_oval(x, y, width, height, resolution=32):
points = [x, y,
x+width, y,
x+width, y+height,
x, y+height,
x, y]
return c.create_polygon(points, fill='#f00', smooth=True, splinesteps=resolution)
def poly_roundrect(x, y, width, height, radius, resolution=32):
#this is not a true round rect
#it's a round rect with the potential to have invisible garbage that cheats
#it cheats convincingly if you don't use an outline when it is cheating
radius = min(min(width, height), radius*2)
points = [x, y,
x+radius, y,
x+(width-radius), y,
x+width, y,
x+width, y+radius,
x+width, y+(height-radius),
x+width, y+height,
x+(width-radius), y+height,
x+radius, y+height,
x, y+height,
x, y+(height-radius),
x, y+radius,
x, y]
rect = c.create_polygon(points, fill='#1f1', smooth=True, splinesteps=resolution)
#display vertices
#for i in range(0, len(points), 2):
# poly_oval(points[i]-2, points[i+1]-2, 4, 4)
return rect
rect = poly_roundrect(10, 10, 300, 50, 10, 64)
root.mainloop()