import turtle
import tkinter as tk
def koch(t, n):
"""Draws a koch curve with length n."""
if n < 10:
t.fd(n)
return
m = n/3
koch(t, m)
t.lt(60)
koch(t, m)
t.rt(120)
koch(t, m)
t.lt(60)
koch(t, m)
def snowflake(t, n):
"""Draws a snowflake (a triangle with a Koch curve for each side)."""
for i in range(3):
koch(t, n)
t.rt(120)
def kochCanvas():
bob.pu()
bob.goto(-150, 90)
bob.pd()
snowflake(bob, 300)
root = tk.Tk()
canvas = tk.Canvas(master = root, width = 500, height = 500)
canvas.pack()
bob = turtle.RawTurtle(canvas)
bob.pencolor("#ff0000") # Red
bob.penup() # Regarding one of the comments
bob.pendown() # Regarding one of the comments
tk.Button(master = root, text = "開始", command = kochCanvas).pack(side = tk.BOTTOM)
root.mainloop()