Python Recursion Tkinter

1

I have the following code to generate circles recursively one inside the other. My problem is that only 2 circles are generated

import tkinter as tk
import sys

def dibujar_circulo(inicio, fin, delta):
    c.create_oval(inicio,inicio,fin,fin,fill="#ffffff",outline="#000000")

    inicio += delta 
    fin    -= delta 
    if(fin >= delta):
        print(str(inicio) + ', ' + str(fin))
        dibujar_circulo(inicio, fin, delta)

if(len(sys.argv)<3):
    print("Necesito tres argumentos: 1.- Inicio, 2.- Fin, 3.- Delta")
    sys.exit(1)
else:
    inicio=int(sys.argv[1]) #1
    fin=int(sys.argv[2])    #600
    delta=int(sys.argv[3])  #5

    if(delta<1):
        delta=5

    root=tk.Tk()
    c=tk.Canvas(root,width=600,height=600)

    dibujar_circulo(inicio, fin, delta)

    c.pack()
    c.mainloop()
    
asked by Franco De León 13.02.2018 в 22:31
source

1 answer

0

The problem is that you draw twice as many circles as you should, and they all have an opaque fill.

I explain. With the parameters you indicate (1, 600, 5) the radius of the outer circle would be 300 (half of 600), so 300/5 = 60 concentric circles should be drawn. That is, stop when you reach the center circle.

However, just as you have done, once you have reached the center circle you continue drawing circles, now increasing, until the last circle uses inicio=596 , fin=5 . Each one of these circles, due to the white opaque filling, is covering those previously drawn so that only two are visible at the end (since the initial one, the exterior, is not redrawn).

You have two solutions: eliminate the fill= parameter when doing each circle, so that during the redrawing they do not clog, or change the condition that ends the recursive calls, so that it is abs(fin-inicio)/2>delta .

For example, to generate the following figure I have set the condition that I just indicated, and as a fill color #ffffcc so that it is not white and so it looks like I have used a fill:

    
answered by 13.02.2018 в 22:53