Nov-21-2019, 11:56 PM
Hello,
First of all, i'm new here !
Then, I wasn't sure if this post belongs to GUI or general coding issue, kind of both.
I'm creating a GUI and I'm having an issue to create many buttons which are supposed to call a single function with the unique ID of the selected user.
My code :
I think it's because the Lambda function is compiled only after the for loop (Emmannuelle is the last ID so it's the value taken into account at the end) but I really don't know how to make it work.
I can't make it outside of my for loop because I don't know how many users I'll have to display
Any idea ?
Thanks !
First of all, i'm new here !
Then, I wasn't sure if this post belongs to GUI or general coding issue, kind of both.
I'm creating a GUI and I'm having an issue to create many buttons which are supposed to call a single function with the unique ID of the selected user.
My code :
i = 0 for key, name in self.__model.get_6_users().items(): print("key_menu = " + str(key) + '-' + name) Button(acceuil, text=name + '-' +str(key), command=lambda: self.click_user(key)).\ grid(column=i, row=2, sticky=N + S + W + E) i += 1Everything is good for the display and with the debugging printing :
Output:key_menu = 2-Eric
key_menu = 5-Batman
key_menu = 0-Bob
key_menu = 4-PierreAFeu
key_menu = 1-Joe
key_menu = 3-Emmanuelle
But when I click on any of them, it's always the 3rd ID (Emmanuelle) which is sent to click_user.I think it's because the Lambda function is compiled only after the for loop (Emmannuelle is the last ID so it's the value taken into account at the end) but I really don't know how to make it work.
I can't make it outside of my for loop because I don't know how many users I'll have to display
Any idea ?
Thanks !