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
  Problem with Submit button Tkinter Reldaing 2 122 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  tkinter button not accessing the command when clicked jhf2 1 183 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 321 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [Tkinter] Tkinter button help and general commands Iceman 1 300 Sep-01-2019, 05:09 PM
Last Post: Yoriz
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 504 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 744 May-12-2019, 08:17 PM
Last Post: joe_momma
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 1,438 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] Help with tkinter; images and button commands SheeppOSU 2 652 Mar-28-2019, 02:01 AM
Last Post: woooee
  Tkinter Button Settings kaanertugrul 3 1,303 Jan-25-2019, 03:41 PM
Last Post: Larz60+
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 8,132 Aug-05-2018, 11:01 PM
Last Post: woooee

Forum Jump:

Users browsing this thread: 1 Guest(s)