lst[j+1] : lst[j] , lst[j+1] = lst[j+1] , lst[j] label = Label(text = str(lst)) label.pack() buttonStart = Button(text="Sort" , command=sort) buttonStart.pack() root.mainloop()"> lst[j+1] : lst[j] , lst[j+1] = lst[j+1] , lst[j] label = Label(text = str(lst)) label.pack() buttonStart = Button(text="Sort" , command=sort) buttonStart.pack() root.mainloop()"> lst[j+1] : lst[j] , lst[j+1] = lst[j+1] , lst[j] label = Label(text = str(lst)) label.pack() buttonStart = Button(text="Sort" , command=sort) buttonStart.pack() root.mainloop()">
from tkinter import * 

root = Tk() 
root.title("Sorting algorithm ")
root.geometry("400x400")

def sort() : 
    lst = [4,23,5,3,4,5,632]

    for i in range(len(lst)-1):
        for j in range(len(lst)-i-1):
            if lst[j] > lst[j+1] : 
                lst[j] , lst[j+1] = lst[j+1] , lst[j]
        
        label = Label(text = str(lst))
        label.pack()

buttonStart = Button(text="Sort" , command=sort)
buttonStart.pack()

root.mainloop()

image.png

Random color question


from tkinter import * 
import random 

root = Tk() 
root.title("Random pattern")
root.geometry("300x300")

button11 = Button(text="")
button11.grid(row=1 , column = 1)
button12 = Button(text="")
button12.grid(row=1 , column = 2)
button13 = Button(text="")
button13.grid(row=1 , column = 3)
button14 = Button(text="")
button14.grid(row=1 , column = 4)

button21 = Button(text="")
button21.grid(row=2 , column = 1)
button22 = Button(text="")
button22.grid(row=2 , column = 2)
button23 = Button(text="")
button23.grid(row=2 , column = 3)
button24 = Button(text="")
button24.grid(row=2 , column = 4)

button31 = Button(text="")
button31.grid(row=3 , column = 1)
button32 = Button(text="")
button32.grid(row=3 , column = 2)
button33 = Button(text="")
button33.grid(row=3 , column = 3)
button34 = Button(text="")
button34.grid(row=3 , column = 4)

button41 = Button(text="")
button41.grid(row=4 , column = 1)
button42 = Button(text="")
button42.grid(row=4 , column = 2)
button43 = Button(text="")
button43.grid(row=4 , column = 3)
button44 = Button(text="")
button44.grid(row=4 , column = 4)

buttons = [button11 , button12 , button13 , button14]
colors = ["red" , "green" , "blue"]

def applyRandomColor(x):
    if x < 0 : 
        return 

    buttons[x]["bg"] = random.choice(colors)
    applyRandomColor(x-1)

applyRandomColor(3)

root.mainloop()

image.png