前言
本篇我们制作一个项目,左下方有一个tkinter滑杆,可以调整正多边形的边数。
效果展示
代码
import tkinter as tk
import math
root = tk.Tk()
w = tk.Canvas(root, width = 1200, height = 670,highlightthickness=0)
w.pack()
x = 600
y = 300
r = 290
n=3
points = []
for i in range(n):
points.append(x - r * math.sin(2 * math.pi * i / n))
points.append(y - r * math.cos(2 * math.pi * i / n))
c=w.create_polygon(points, outline = "green", fill = "yellow")
def flush(get):
global c
n=int(get)
w.delete(c)
points = []
for i in range(n):
points.append(x - r * math.sin(2 * math.pi * i / n))
points.append(y - r * math.cos(2 * math.pi * i / n))
c=w.create_polygon(points, outline = "green", fill = "yellow")
s = tk.Scale(w,orient='horizontal',from_=3,to=33,command=flush,length=600,tickinterval=5,foreground='blue').place(x=15,y=600)
root.mainloop()
尾声
好了,本篇到这里就结束了,欢迎评论,喜欢就点个关注吧!