import math from time import sleep import tkinter as tk root = tk.Tk() def draw_Triangle(startPoint, endPoint1, endPoint2): canvas.create_polygon(startPoint, endPoint1, endPoint2, outline='red', fill='white', width=2) def Triangle_Pattern(): #绘制基本图案按钮所调用的函数 canvas.delete("all") # 清除画布 startPoint=(80,130); endPoint1=(20,10); endPoint2=(140,10) draw_Triangle(startPoint, endPoint1, endPoint2) def Triangle_tree(depth, percent, start_point, angle, length, canvas): if depth == 0: return # 左枝点和右枝点 end_point1 = (start_point[0] - length * math.sin(math.radians(angle)), start_point[1] - length * math.cos(math.radians(angle))) end_point2 = (start_point[0] + length * math.sin(math.radians(angle)), start_point[1] - length * math.cos(math.radians(angle))) # 画三角形 draw_Triangle(start_point, end_point1, end_point2) # 递归画左枝和画右枝 Triangle_tree(depth - 1, percent, end_point1, angle, length * percent, canvas) Triangle_tree(depth - 1, percent, end_point2, angle, length * percent, canvas) canvas.update() sleep(0.001) if __name__ == '__main__': canvas = tk.Canvas(root, width=1200, height=1000) canvas.pack() FinishLevel = 8 TreePercent = 0.8 InitPoint = (520, 650) InitAngle = 30 InitLong = 170 Triangle_tree(FinishLevel, TreePercent, InitPoint, InitAngle, InitLong, canvas)