I am building a basic GUI and I want to be able to assign each item in my array to a button. The buttons are created via a foreach loop.
I am trying to make the button display its respective letter on click.
Originally, I thought simply adding a "command" attribute to the button would create the association I needed. This only prints a list of all of letters. I don't want it print every letter, but simply print whichever letter of the button I click
Below is my current code.
alphabet = ["A", "B", "C", "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N", "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z", " ", " "]
count = 0
for r in range(7):
for c in range(4):
tk.Button(self.searchFrame, text=alphabet[count], height='4', width='8', bg='white', fg='deep sky blue',
font=("Helvetica", 18),command=self.listitems(alphabet[count])).grid(row=r,column=c)
count += 1
def listitems(self, letter):
print(letter)
I am expecting each button to display their respective letter on click.
This is what the GUI looks like
lambda: self.listitems(alphabet[count])as you can't pass parameters like that to a function. - Saad