Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Tkinter button in for

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 += 1
Everything is good for the display and with the debugging printing :
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 !
key is no longer in scope after you exit the for loop,
therefore there's no way to tell what it's value will be when the button is clicked.
Well that's kind of what I had in mind...

So I changed my code and now it works but it's awful :(

        keys = []
        names = []
        for key, name in self.__model.get_6_users().items():
        for i in range(len(keys), 6):
        Button(acceuil, text=names[0], command=lambda: self.click_user(keys[0])). \
            grid(column=0, row=2, sticky=N + S + W + E)
        Button(acceuil, text=names[1], command=lambda: self.click_user(keys[1])). \
            grid(column=1, row=2, sticky=N + S + W + E)
        Button(acceuil, text=names[2], command=lambda: self.click_user(keys[2])). \
            grid(column=2, row=2, sticky=N + S + W + E)
        Button(acceuil, text=names[3], command=lambda: self.click_user(keys[3])). \
            grid(column=3, row=2, sticky=N + S + W + E)
        Button(acceuil, text=names[4], command=lambda: self.click_user(keys[4])). \
            grid(column=4, row=2, sticky=N + S + W + E)
        Button(acceuil, text=names[5], command=lambda: self.click_user(keys[5])). \
            grid(column=5, row=2, sticky=N + S + W + E)
        for i in range(0,len(keys)):
            if keys[i] == '-':
                Button(acceuil, text=names[i], state=DISABLED).grid(column=i, row=2, sticky=N + S + W + E)
So I get my keys and names that I put in two != lists and I and values '-' if I don't have enough user to fill the 6 fields.
Then I create my 6 buttons.
Finaly I overwrite the buttons where '-' is set

If you have any better suggestions...?
post the entire script and I'll take a look.
I need something that I can run

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to make button text bold in Tkinter? scratchmyhead 2 100 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] Is it possible to automate button generation in tkinter? FirePepi 3 214 Apr-10-2020, 12:14 PM
Last Post: Riddle
  Tkinter:Unable to bind and unbind function with a button shallanq 2 218 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  Returning a value from a tkinter.button call markr0804 4 788 Feb-16-2020, 10:35 AM
Last Post: markr0804
  Problem with Submit button Tkinter Reldaing 2 297 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  tkinter button not accessing the command when clicked jhf2 1 364 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 787 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [Tkinter] Tkinter button help and general commands Iceman 1 490 Sep-01-2019, 05:09 PM
Last Post: Yoriz
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 830 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 990 May-12-2019, 08:17 PM
Last Post: joe_momma

Forum Jump:

Users browsing this thread: 1 Guest(s)