1

Using a for loop to create a list of buttons, and when clicked the button updates it's text to say "unavailable". My code below will only update the latest button, and not the one specified

from tkinter import *

root = Tk()

list = ["button 1 available", "button 2 available", "button 3 available"]


def update(item):
    btn["text"] = item.replace("available", "unavailable")


for item in list:
    btn = Button(root, text=item,  command=lambda : update(item))
    btn.pack()

root.mainloop()
Sean Lee
  • 19
  • 5
  • Does this answer your question? [Tkinter assign button command in loop with lambda](https://stackoverflow.com/questions/17677649/tkinter-assign-button-command-in-loop-with-lambda) – Henry Yik Sep 04 '20 at 05:45

2 Answers2

1

The FOR loop you use will eventually change the 'btn' variable to the button with 'button 3 available' text. My solution for this is to create another function which creates an individual button:

from tkinter import *

root = Tk()

list = ["button 1 available", "button 2 available", "button 3 available"]

# Function to change button text
def update(item, btn):
    btn["text"] = item.replace("available", "unavailable")

# Function to create button
def createButton(item): 
    btn = Button(root, text=item, command=lambda: update(item, btn))
    btn.pack()

# Updated for loop
for item in list: 
    createButton(item)
Celvin
  • 11
  • 3
0

This should work for your needs.

from tkinter import *

root = Tk()

buttonTextList = ["button 1 available", "button 2 available", "button 3 available"]

def update(item, btn):
    btn["text"] = item.replace("available", "unavailable")

# Function to create button
def createButton(item): 
    btn = Button(root, text=item, command=lambda: update(item, btn))
    btn.pack()

# Updated for loop
for item in buttonTextList: 
    createButton(item)
P S Solanki
  • 1,033
  • 2
  • 11
  • 26
  • 1
    Sorry for the late answer, I used Celvin's solution but after a little testing eventually came to conclusion yours would be best in the long run. I'm working on a personal project that has gone from 50 lines of code to about 200. Thank you – Sean Lee Sep 07 '20 at 11:02
  • Both the answers use similar approaches, I just did a bit more modifications (or generalizations of the code). Would appreciate it if you upvote the answer by @celvin as well, because he tried (his code works now as well) and he just has 1 rep :) (now 11 after my upvote). although it doesn't matter that much but it gives people the motivation to help others. – P S Solanki Sep 07 '20 at 11:25
  • 1
    I would upvote everyone who helps whether attempting to answer my question or somebody else's question. But my stackoverflow Reputation is 3 so I get a "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Sean Lee Sep 07 '20 at 12:46
  • @Sean Oh yeah, I keep forgetting that (I actually didn't check your rep score). You will be there soon :).... Good Luck on your project – P S Solanki Sep 07 '20 at 12:48
  • 1
    Thanks, I'll go back to upvote all online strangers who helped me... the least I can do ;) – Sean Lee Sep 07 '20 at 12:51