Im attempting to make an undo feature for this project. How would you track the variables of color while pinning them to their respective button? Im very new to the space so if you need any clarifications I would be happy to give. One possible way I can imagine doing so is indexing each individual buttons color at every loop to a list and then [:-1] the list to go to the previous version of the grid. I shortened the code by removing the grid of buttons while keeping b1. So in the actual version there b1-b20 and a few more colors but it took over 100 lines so Id rather save your eyes.
import tkinter as tk
color = "white"
root = tk.Tk()
root.title("Paint")
canvas = tk.Canvas(root, width = 0, height = 0, bg = "white")
canvas.grid(row = 0,column =0)
def blue_c():
global color
color = "blue"
def red_c():
global color
color = "red"
def yellow_c():
global color
color = "yellow"
def green_c():
global color
color = "green"
def black_c():
global color
color = "black"
def brown_c():
global color
color = "brown"
def b_click(button):
button['background']=color
button['activebackground']=color
def fill_c():
b1['background']= color
b1['activebackground']=color
def newgame_click():
global color
color = "white"
fill_c()
def erase_c():
global color
color = "white"
erasebutton= tk.Button(root, text="Erase", height=2, width =5, command = erase_c)
erasebutton.grid(row = 5, column = 2)
bluebutton= tk.Button(root, text="Blue", height=2, width =5, command = blue_c)
bluebutton.grid(row = 1, column = 7)
redbutton= tk.Button(root, text="Red", height=2, width =5, command = red_c)
redbutton.grid(row = 2, column = 7)
orangebutton= tk.Button(root, text="Orange", height=2, width =5, command = orange_c)
orangebutton.grid(row = 5, column = 8)
yellowbutton= tk.Button(root, text="Yellow", height=2, width =5, command = yellow_c)
yellowbutton.grid(row = 3, column = 7)
greenbutton= tk.Button(root, text="Green", height=2, width =5, command = green_c)
greenbutton.grid(row = 4, column = 7)
brownbutton= tk.Button(root, text="Brown", height=2, width =5, command = brown_c)
brownbutton.grid(row = 1, column = 8)
b1= tk.Button(root, background = "white",activebackground = "white", text="", height=5, width =5, command =lambda:b_click(b1))
b1.grid(row = 1, column = 1)
root.mainloop()