【发布时间】:2019-11-09 03:25:33
【问题描述】:
我想重现here 所见的美丽摩洛哥马赛克。代码使用的是 Turtle。我想把它翻译成 Tkinter。我很幸运地修改了答案here 中的代码以绘制到画布上。当然有很多缺失,因为没有从原点绘制线。我不知道该怎么做。一个想法是我可能会覆盖 n 个多边形,每个多边形虽然旋转了一些 2 * pi/n 度。这仍然留下了将线从原点获取到每个多边形的初始点和最终点的问题。我认为这并不能完全解决所有问题,但这是一个很好的开始。
或者以某种方式让多边形围绕中心旋转可能更简单?
from tkinter import *
import math
canvas_width = 400
canvas_height =400
python_green = "#476042"
def polygon(canvas,sides=10, radius=100, rotation=0, translation=None, outline=python_green, fill='White', width = 1):
one_segment = math.pi * 2 / sides
points = [
(math.sin(one_segment * i + rotation) * radius + canvas_width/2,
math.cos(one_segment * i + rotation) * radius + canvas_height/2)
for i in range(sides)]
if translation:
points = [[sum(pair) for pair in zip(point, translation)]
for point in points]
canvas.create_polygon(points, outline=outline, fill='', width=width)
master = Tk()
w = Canvas(master, width=canvas_width, height=canvas_height)
w.pack()
polygon(w,outline='green', width=2)
mainloop()
【问题讨论】:
标签: tkinter drawing tkinter-canvas