I am working on a sorting visualization program with tkinter using the height of different rectangles. I was able to randomize the height of each rectangle, but now I am stuck trying to figure out how to switch the position of two rectangles with each other. I need to figure out how to do this before I can start trying to implement a sort.
Here is what I have so far:
import tkinter as tk
import random
window = tk.Tk()
window.title('Sorting')
window.geometry('600x400')
xstart = 5
xend = 15
canvas = tk.Canvas(window, width='600', height='400')
canvas.pack()
barList = []
lengthList = []
for x in range(1,60):
randomY = random.randint(1,390)
bar = canvas.create_rectangle(xstart,randomY,xend,395, fill='red')
barList.append(bar)
xstart += 10
xend +=10
for bar in barList:
x = canvas.coords(bar)
length = x[3]-x[1]
lengthList.append(length)
window.mainloop()
Here is a small example to get you started: It swaps rectangle at position 10 and rectangle at position 20 upon clicking on the swap button.