How to set the variablee in a button command to own button's text in a loop that creates multipule buttons? [duplicate]
def print_button_pressed_text(num): def handler: return num; return handler;
UPD
The problem is that lambda functions look at the wrong button, only the last one since the loop doesn't provide the right scope.
def make_button(text, row): b = Button(app, text=text) b.configure(command=lambda: print_button_pressed_text(b.cget('text'))) b.grid(row=x+1, column=1) return bwhile l >= 0: make_button(feed[l], x + 1) # ...
UPD2
Why does it happen? Please, check this out → https://louisabraham.github.io/articles/python-lambda-closures.html if you want to know more about scopes and closures.
Can't you use the join function ?Just define a function:
feed = ["Cooking", "Sports", "Tv", "Fashion"]def print_the_list: global feed print(" ".join(feed))print_the_list()
Then set this function as your button's command. If you want to put , or ; or whatever you want between the elements, modify the " " into "," or ";"