[Python] Python Tkinter Buttons: Passing arguments via lambda is not working

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get the input text from an entry and based upon the length of the text, perform some other things. My code looks like this:

    entry = tk.Entry(root)
    entry.grid(row=8, column=7, rowspan=2)
    entry_text = entry.get().upper()
    print(x)
    button = tk.Button(root, text="Enter the Word", command= lambda: get_entry_text(entry_text, i_label_list))
    button.grid(row=8, column=8, rowspan=2)


    My lambda function looks like this:

    def get_entry_text(entry_value, i_label_list):
    global no_of_attempts
    print(len(entry_value))
    if len(entry_value) == 6:
    no_of_attempts += 1
    for i in range(6):
    i_label_list[no_of_attempts].config(text=entry_value)
    else:
    print('Invalid Length')


    What I have discovered is that the lambda function is not getting/passing the actual text in the entry. Rather, it is getting/passing empty text to the get_entry_text function, which results in 'invalid length' no matter which text I pass.

    I have tried partial from functools to pass the values, but to no avail.

    What am I doing wrong?

    Continue reading...

Compartilhe esta Página