0
from tkinter import *
root = Tk()
root.config(bg = "white")

list = ["a", "b", "c"]

for i in list:
    Button(root, text = i,command = i).pack()

root.mainloop()

After running this program, I get the command of every button as "c". But I need each buttons command as each element of list.

Friedrich
  • 2,011
  • 2
  • 17
  • 19
  • Does this answer your question? https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments – Friedrich Jan 26 '23 at 07:39
  • The obvious problem that remains is that `"c"` is a string, not a function. – Friedrich Jan 26 '23 at 07:48
  • Why do you assign a string to the `command` option of a button? It doesn't make sense. – acw1668 Jan 26 '23 at 09:44
  • @DelriusEuphoria I don't think it is a duplicate of the linked question because OP does not reference `i` inside a `lambda`. It just assign a string to the `command` option and it will not produce the issue mentioned in the question and the linked question. – acw1668 Jan 26 '23 at 09:47
  • BTW `list` is a builtin and should not be used as a variable name. – Friedrich Jan 26 '23 at 10:05
  • What is supposed to happen when the user clicks any of the buttons? All we can do is guess. – Friedrich Jan 26 '23 at 10:09
  • @acw1668 I assumed that was a typo as no "real" function is mentioned, prolly the OP missed out on it – Delrius Euphoria Jan 26 '23 at 10:23

0 Answers0