Jan-18-2022, 03:54 PM
I'm trying to make a button with a simple function with a variable defined in another function. I want the program to check if the key the user entered is in a list or not, and show a message box accordingly, using get(). It shows error even if the key is in the valid_keys list. After trying several things, I thought using the lambda function was the solution, but it isn't. What am I doing wrong?
from tkinter import * from tkinter import messagebox root = Tk() root.title("Software") valid_keys = ['34B2-0A4B-3B98-A701', '330D-1ADE-C44D-1F1C'] def enter_key(): register = Toplevel() register.title("Register") E1 = Entry(register, bd=5, width=30) E1.grid(row=0, column=1) user_key = E1.get() L1 = Label(register, text="Enter Licence Key:") L1.grid(row=0,column=0) validate = Button(register, text="Validate", command=lambda:validate_key(user_key)) validate.grid(row=0,column=2) def validate_key(user_key): if user_key in valid_keys: messagebox.showinfo("Thank you", "Thank you. Enjoy the program!") else: messagebox.showerror("Error", "Invalid licence key") myLabel1 = Label(root, text="Welcome to Software") myLabel2 = Label(root, text="Text") enter_licence_key = Button(root, text="Enter Licence Key", command=enter_key) trial_version = Button(root, text="Trial (30 days)") myLabel1.grid(row=0, column=0) myLabel2.grid(row=1, column=0) enter_licence_key.grid(row=3, column=0) trial_version.grid(row=3, column=1) mainloop()