Python Forum
python and tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python and tkinter
#1
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.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
Try
from functools import partial
...
... command=partial(print, letter)
Reply
#3
Just figured it out. Thanks for the reply though. did lambda letter=letter:print(letter)

Hey thanks didn't know about partial. thanks again
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020