python - 如何在tkinter上绘制递归树

标签 python recursion tkinter

我尝试根据用户输入的深度在 python tkinter 上绘制递归树,这是迄今为止我的代码:

from tkinter import * # Import tkinter
import math

#angleFactor = math.pi/5
#sizeFactor = 0.58

class Main:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Recursive Tree") # Set a title

        self.width = 200
        self.height = 200
        self.canvas = Canvas(window, 
        width = self.width, height = self.height,bg="white")
        self.canvas.pack()

        # Add a label, an entry, and a button to frame1
        frame1 = Frame(window) # Create and add a frame to window
        frame1.pack()

        Label(frame1, 
        text = "Enter the depth: ").pack(side = LEFT)
        self.depth = StringVar()
        entry = Entry(frame1, textvariable = self.depth, 
                  justify = RIGHT).pack(side = LEFT)
        Button(frame1, text = "Display Recursive Tree", 
        command = self.display).pack(side = LEFT)

        self.angleFactor = math.pi/5
        self.sizeFactor = 0.58          

        window.mainloop() # Create an event loop

    def drawLine(self, x1,x2,y1,y2):
        self.canvas.create_line(x1,y1,x2,y2, tags = "line")    

    def display(self):
        self.canvas.delete("line")
        return self.paintBranch(int(self.depth.get()),self.width/2, self.height /2   , self.height/3, math.pi/2)

    def paintBranch(self,depth, x1, y1, length, angle):

        if depth >= 0:

            x2 = x1 +int( math.cos(angle) * length)
            y2 = y1 + int(math.sin(angle) * length)


            # Draw the line
            self.drawLine(x1,y1,x2,y2)


            # Draw the left branch
            self.paintBranch(depth - 1, x2, y2, length * self.sizeFactor, angle + self.angleFactor  )
            # Draw the right branch
            self.paintBranch(depth - 1, x2, y2, length * self.sizeFactor, angle - self.angleFactor )        


Main()

当用户输入深度=0时,代码可以正常工作,但是当在深度>=1中递归时,代码无法绘制树,我需要帮助我的代码,谢谢之前

最佳答案

您的主要问题是您搞乱了 drawLine 方法的参数。您将其定义为

drawLine(self, x1,x2,y1,y2)

但你称其为

self.drawLine(x1,y1,x2,y2)

因此,您传递的 y1 参数将用于 x2 参数,而 x2 参数将用于 y1 参数。

您还需要更改初始 y1 值,并在从 y1 计算 y2 时更改符号,因为 Tkinter 中的 Y 坐标当您向下移动屏幕时增加。

这是代码的修复版本。

from tkinter import * # Import tkinter
import math

class Main:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Recursive Tree") # Set a title

        self.width = 400
        self.height = 400
        self.canvas = Canvas(window, 
        width = self.width, height = self.height,bg="white")
        self.canvas.pack()

        # Add a label, an entry, and a button to frame1
        frame1 = Frame(window) # Create and add a frame to window
        frame1.pack()

        Label(frame1, 
            text = "Enter the depth: ").pack(side = LEFT)
        self.depth = StringVar()
        Entry(frame1, textvariable = self.depth, 
            justify = RIGHT).pack(side = LEFT)
        Button(frame1, text = "Display Recursive Tree", 
            command = self.display).pack(side = LEFT)

        self.angleFactor = math.pi/5
        self.sizeFactor = 0.58

        window.mainloop() # Create an event loop

    def drawLine(self, x1,y1, x2,y2):
        self.canvas.create_line(x1,y1, x2,y2, tags = "line")    

    def display(self):
        self.canvas.delete("line")
        depth = int(self.depth.get())
        return self.paintBranch(depth, self.width/2, self.height, self.height/3, math.pi/2)

    def paintBranch(self, depth, x1, y1, length, angle):
        if depth >= 0:
            depth -= 1
            x2 = x1 + int(math.cos(angle) * length)
            y2 = y1 - int(math.sin(angle) * length)

            # Draw the line
            self.drawLine(x1,y1, x2,y2)

            # Draw the left branch
            self.paintBranch(depth, x2, y2, length * self.sizeFactor, angle + self.angleFactor  )
            # Draw the right branch
            self.paintBranch(depth, x2, y2, length * self.sizeFactor, angle - self.angleFactor )        


Main()

关于python - 如何在tkinter上绘制递归树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39266081/

相关文章:

python - 使用图像和标签的数据帧创建 Tensorflow 数据集

python - 在 Google App Engine 上使用 python 使用 POST 方法处理 HTML 表单数据

python - 在 tkinter 中将单个像素绘制到 Canvas 上

Python tkinter 销毁窗口

python - 如何将以下 if 语句转换为更有效的嵌套 for 循环

Python 3.4 strptime() 不工作

javascript - 这个在树中查找节点的简单递归函数有什么问题?

c++ - 递归的想法

java - 递归中的前缀无法正常工作

Python TKinter : how to delete multiple widgets by the same name created in a for loop?