Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter and lambda
#1
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()
Reply


Messages In This Thread
Tkinter and lambda - by Cristopher - Jan-18-2022, 03:54 PM
RE: Tkinter and lambda - by Yoriz - Jan-18-2022, 04:18 PM
RE: Tkinter and lambda - by menator01 - Jan-18-2022, 04:27 PM
RE: Tkinter and lambda - by menator01 - Jan-18-2022, 05:07 PM
RE: Tkinter and lambda - by Cristopher - Jan-18-2022, 05:47 PM

Forum Jump:

User Panel Messages

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