I am trying to write a program of a bingo grid and want to reduce my code down to make it simpler. I am currently repeating this code:
from tkinter import *
from tkinter import Tk, Button, Label
mycolor = '#FF7F50'
mycolor2 = '#FFFFFF'
mycolor3 = '#BAF0FF'
class Window:
def __init__(self, master):
self.master = master
self.master.title("Bingo")
self.master.minsize(width=1920, height=1080)
self.master.config(bg=mycolor3)
self.A = Button(master, text='1', font=('Helvetica', '23'), height=1, width=20, command=self.toggleA, bg=mycolor2)
self.A.grid(column=0,row=1)
self.B = Button(master, text='2', font=('Helvetica', '23'), height=1, width=20, command=self.toggleB, bg=mycolor2)
self.B.grid(column=0,row=2)
def toggleA(self):
self.A.config('bg')
if self.A.config('bg')[-1] == mycolor2:
self.A.config(bg=mycolor)
else:
self.A.config(bg=mycolor2)
def toggleB(self):
self.B.config('bg')
if self.B.config('bg')[-1] == mycolor2:
self.B.config(bg=mycolor)
else:
self.B.config(bg=mycolor2)
root = Tk()
my_win = Window(root)
root.mainloop()
and using this exactly, I have to repeat this script 75 times by changing the variable names to complete the program, this works, but i want to know if there is a way to the same definition of toggle for each button opposed to defining a new toggle for each button? The toggle is used to affect the color of the button toggling it from one color to the other and Im not sure how to get the same commands to be called by all the buttons to affect each button individually. thank you!