Hello! my name is Quentin and i am a young french programmer and i have some issue with my program i don't know how to simplify it for the refresh function and the creation of rectangles.
i would like to create a fonction that create X rectangles for me
then i will apply this to my program to then apply it to a sorting algorithm to make a sorting visualizer

Here is my code:

from tkinter import *
from random import randint

def rander():
    global numcolone,hauteur
    if numcolone == 0:
        canvas.coords(rectangle0,2,500,7,hauteur[0])
    elif numcolone == 1:
        canvas.coords(rectangle1,9,500,14,hauteur[1])
    elif numcolone == 2:
        canvas.coords(rectangle2,16,500,21,hauteur[2])
    elif numcolone == 3:
        canvas.coords(rectangle3,23,500,28,hauteur[3])
    elif numcolone == 4:
        canvas.coords(rectangle4,30,500,35,hauteur[4])
    elif numcolone == 5:
        canvas.coords(rectangle5,37,500,42,hauteur[5])
    elif numcolone == 6:
        canvas.coords(rectangle6,44,500,49,hauteur[6])
    elif numcolone == 7:
        canvas.coords(rectangle7,51,500,56,hauteur[7])
    elif numcolone == 8:
        canvas.coords(rectangle8,58,500,63,hauteur[8])
    elif numcolone == 9:
        canvas.coords(rectangle9,65,500,70,hauteur[9])
    elif numcolone == 10:
        canvas.coords(rectangle10,70,500,77,hauteur[10])

def refresh():
    canvas.coords(rectangle0,2,500,7,hauteur[0])
    canvas.coords(rectangle1,9,500,14,hauteur[1])
    canvas.coords(rectangle2,16,500,21,hauteur[2])
    canvas.coords(rectangle3,23,500,28,hauteur[3])
    canvas.coords(rectangle4,30,500,35,hauteur[4])
    canvas.coords(rectangle5,37,500,42,hauteur[5])
    canvas.coords(rectangle6,44,500,49,hauteur[6])
    canvas.coords(rectangle7,51,500,56,hauteur[7])
    canvas.coords(rectangle8,58,500,63,hauteur[8])
    canvas.coords(rectangle9,65,500,70,hauteur[9])
    canvas.coords(rectangle10,70,500,77,hauteur[10])

def clavier(event):
    global hauteur,numcolone
    touche = event.keysym
    if touche == "Up":
        hauteur[numcolone] = hauteur[numcolone] - 10
    elif touche == "Down":
        hauteur[numcolone] = hauteur[numcolone] + 10
    elif touche == "Left" and numcolone>0:
        numcolone = numcolone -1
    elif touche == "Right" and numcolone<10:
        numcolone = numcolone +1
    rander()


def rand():
    global hauteur
    for i in range(len(hauteur)):
        hauteur[i]=randint(0,490)
    refresh()

fenetre=Tk()
canvas = Canvas(fenetre,width=79,height= 500,bg='black')
numcolone=0
hauteur=[10,10,10,10,10,10,10,10,10,10,10]
rectangle0 = canvas.create_rectangle(2,500,7,10,fill= 'ivory')
rectangle1 = canvas.create_rectangle(9,500,14,10,fill= 'ivory')
rectangle2 = canvas.create_rectangle(16,500,21,10,fill= 'ivory')
rectangle3 = canvas.create_rectangle(23,500,28,10,fill='ivory')
rectangle4 = canvas.create_rectangle(30,500,35,10,fill='ivory')
rectangle5 = canvas.create_rectangle(37,500,42,10,fill='ivory')
rectangle6 = canvas.create_rectangle(44,500,49,10,fill='ivory')
rectangle7 = canvas.create_rectangle(51,500,56,10,fill='ivory')
rectangle8 = canvas.create_rectangle(58,500,63,10,fill='ivory')
rectangle9 = canvas.create_rectangle(65,500,70,10,fill='ivory')
rectangle10 = canvas.create_rectangle(72,500,77,10,fill='ivory')
canvas.focus_set()
canvas.bind("<Key>",clavier)
canvas.pack()
bouton = Button(fenetre,text="fermer",command=fenetre.quit)
bouton.pack(side='left')
random = Button(fenetre,text="random",command=rand)
random.pack(side='right')
fenetre.mainloop()

Hello!

This is a standard pattern when you have numbered variable names var0 var1 var2 etc. The solution is to use a list and var[0], var[1], var[2] etc

from tkinter import *
from random import randint

def rander():
    i = numcolone
    assert 0 <= i < len(rectangle)
    canvas.coords(rectangle[i], 2 + 7 * i, 500, 7 + 7 * i, hauteur[i])

def refresh():
    for i, r in enumerate(rectangle):
        canvas.coords(r, 2 + 7 * i, 500, 7 + 7 * i, hauteur[i])

def clavier(event):
    global numcolone
    touche = event.keysym
    if touche == "Up":
        hauteur[numcolone] = hauteur[numcolone] - 10
    elif touche == "Down":
        hauteur[numcolone] = hauteur[numcolone] + 10
    elif touche == "Left" and numcolone>0:
        numcolone = numcolone -1
    elif touche == "Right" and numcolone<10:
        numcolone = numcolone +1
    rander()


def rand():
    global hauteur
    for i in range(len(hauteur)):
        hauteur[i]=randint(0,490)
    refresh()

fenetre=Tk()
canvas = Canvas(fenetre,width=79,height= 500,bg='black')
numcolone=0
hauteur=[10,10,10,10,10,10,10,10,10,10,10]
rectangle = []
for i in range(11):
    r = canvas.create_rectangle(2 + 7 * i, 500, 7 + 7 * i, 10, fill='ivory')
    rectangle.append(r)
canvas.focus_set()
canvas.bind("<Key>",clavier)
canvas.pack()
bouton = Button(fenetre,text="fermer",command=fenetre.quit)
bouton.pack(side='left')
random = Button(fenetre,text="random",command=rand)
random.pack(side='right')
fenetre.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.