When searching for the answer to this I found a similar post online, which I will link bellow, and their code is what I have been working from.
After reading this I decided I would copy it in to a file and try get it to work and I ended up with this...
from tkinter import *
class testClass:
def main(self):
root = Tk()
frame=Frame(root)
frame.grid(row=0,column=0)
self.btn= [[0 for x in range(20)] for x in range(60)]
for x in range(60):
for y in range(20):
self.btn[x][y] = Button(frame,command= lambda: self.color_change(x,y))
self.btn[x][y].grid(column=x, row=y)
root.mainloop()
def color_change(self,x,y):
self.btn[x][y].config(bg="red")
testMain = testClass()
testMain.main()
However this has taken me back to a very similar place to where I was before. I can only change the color of the most recently made button. How would I fix this?
Here is my old code before I found the last post encase that helps too:
from tkinter import *
main = Tk()
wd = Frame(main, width = "500", height = "500", bg = "brown")
wd.pack( padx = 5, pady = 5)
tile = []
def onClick():
tile[-1].config(bg = "green")
#I have no idea how I would find where the button I clicked is in the array. Might not be possible the way I have done it.
for index in range(8):
for counter in range(8):
if index in range(2, 5):
tile.append(Button(wd, width = 2, height = 1, command = onClick))
tile[-1].grid(padx = 0.5, pady = 0.5, row = index, column = counter)
elif index in range(0, 2):
tile.append(Button(wd, width = 2, height = 1, text = "O", fg = "red", command = onClick))
tile[-1].grid(padx = 0.5, pady = 0.5, row = index, column = counter)
elif index in range(6, 8):
tile.append(Button(wd, width = 2, height = 1, text = "O", fg = "blue", command = onClick))
tile[-1].grid(padx = 0.5, pady = 0.5, row = index, column = counter)
main = mainloop()