Python Forum

Full Version: python and tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey all, I'm kind of new to python and hope you can help me figure this out.

I cant seem to get the letter to pass to the print. Thanks for any help.

def main():
    root = Tk()    
    app = Database()
    alphabet = string.ascii_uppercase
    letters = []
    for letter in alphabet:
        letters.append(Button(text=letter, command=lambda:print(letter)).pack(side="left", padx="1", ipadx="2", ipady="2"))


    ebutton = Button(root, text="Exit", command=quit, fg="red").pack(side="left", padx="1", ipadx="2",ipady="2")

    root.mainloop()
    
if __name__ == '__main__':
    main()

Forgot to add that it will only pass the letter z. The buttons show the right letter. Just the command doesn't.
Try
from functools import partial
...
... command=partial(print, letter)
Just figured it out. Thanks for the reply though. did lambda letter=letter:print(letter)

Hey thanks didn't know about partial. thanks again