How to draw recursive tree on tkinter
<p>I tried to draw recursive tree on python tkinter based on depth enter by user, here is my code so far:</p>

<pre><code>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 &gt;= 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()
</code></pre>

<p>when the user enter <code>depth=0</code> the code is working, but when recursive in <code>depth &gt;=1</code>, the code fail to draw a tree, I need help for my code, thanks before</p>

<p>Your main problem is that you messed up the args to the <code>drawLine</code> method. You defined it as </p>

<pre><code>drawLine(self, x1,x2,y1,y2)
</code></pre>

<p>but you call it as </p>

<pre><code>self.drawLine(x1,y1,x2,y2)
</code></pre>

<p>so the <code>y1</code> arg you pass it gets used for the <code>x2</code> parameter, and the <code>x2</code> arg gets used for the <code>y1</code> parameter. </p>

<p>You also need to change your initial <code>y1</code> value, and to change the sign when computing <code>y2</code> from <code>y1</code>, because Y coordinates in Tkinter increase as you go down the screen.</p>

<p>Here's a repaired version of your code.</p>

<pre><code>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 &gt;= 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()
</code></pre>

python recursion tkinter