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
#2
The lambda has been passed the return value of calling entry's get method so it will only ever contain that value which at that point will be empty as it has only just been created.
If you want to use lambda to get the value at the point of clicking the button you would need to call the get method.
validate = Button(register, text="Validate", command=lambda:validate_key(E1.get()))
Cristopher and BashBedlam like this post
Reply
#3
Example using partial from functools.

#!/usr/bin/env python3
import tkinter as tk
from tkinter import messagebox
from functools import partial


# Valid key list
valid_keys = ['34B2-0A4B-3B98-A701', '330D-1ADE-C44D-1F1C']

# Define a function for validating keys
def validate(entry_key):
    entry_key = entry_key.get()
    if entry_key in valid_keys:
        messagebox.showinfo('Key Registered', 'Thank you for using our product!')
    else:
        messagebox.showerror('Error!', 'Sorry, that key is not valid.')

root = tk.Tk()
root['padx'] = 5
root['pady'] = 4

container = tk.Frame(root)
container.grid(column=0, row=0, sticky='news')

# Create a string variable for pur key
entry_key = tk.StringVar()

label = tk.Label(container, text='Please Enter Licence Key')
label['font'] = ('tahoma 12 bold')
label.grid(column=0, row=0, sticky='new')

entry = tk.Entry(container)
entry['font'] = ('tahome 12 normal')
entry.focus()
entry.grid(column=0, row=1, sticky='new', pady=8)

# Using partial to send our entry field variable to the validate function
btn = tk.Button(container, text='Register Key')
btn['font'] = ('verdana 12 normal')
btn['fg'] = 'navy'
btn['command'] = partial(validate, entry)
btn.grid(column=0, row=2, sticky = 'new')


root.mainloop()
Cristopher likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
I didn't notice that you were using a function to call the register form so, here is a modified version example.

#!/usr/bin/env python3
import tkinter as tk
from tkinter import messagebox
from functools import partial


# Valid key list
valid_keys = ['34B2-0A4B-3B98-A701', '330D-1ADE-C44D-1F1C']

# Define a function for validating keys
def validate(entry_key, parent, window):
    entry_key = entry_key.get()
    if entry_key in valid_keys:
        messagebox.showinfo('Key Registered', 'Thank you for using our product!')
        window.destroy()
        parent.deiconify()
    else:
        messagebox.showerror('Error!', 'Sorry, that key is not valid.')

def cancel(parent, window):
    parent.deiconify()
    window.destroy()

def register(parent):
    parent.iconify()
    window = tk.Toplevel()
    window.title('Register')
    window['padx'] = 8
    window['pady'] = 4

    # Create a string variable for pur key
    entry_key = tk.StringVar()

    label = tk.Label(window, text='Please Enter Licence Key')
    label['font'] = ('tahoma 12 bold')
    label.grid(column=0, row=0, sticky='new')

    entry = tk.Entry(window)
    entry['font'] = ('tahome 12 normal')
    entry.focus()
    entry.grid(column=0, columnspan=2, row=1, sticky='new', pady=8)

    # Using partial to send our entry field variable to the validate function
    btn = tk.Button(window, text='Register Key')
    btn['font'] = ('verdana 12 normal')
    btn['fg'] = 'navy'
    btn['command'] = partial(validate, entry, parent, window)
    btn.grid(column=0, row=2, sticky = 'new', padx=1, pady=3)

    btn_cancel = tk.Button(window, text='Cancel', bg='tomato')
    btn_cancel['command'] = partial(cancel, parent, window)
    btn_cancel['font'] = ('verdana 12 normal')
    btn_cancel.grid(column=1, row=2, sticky='new', padx=1, pady=3)


root = tk.Tk()
root['padx'] = 5
root['pady'] = 4

btn_dict = {'Trial Version': None, 'Register': partial(register, parent=root)}

i=0
for text, command in btn_dict.items():
    btn = tk.Button(root, text=text, command=command)
    btn.grid(column=i, row=0, sticky='new', padx=3)
    i+=1


root.mainloop()
Cristopher and BashBedlam like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(Jan-18-2022, 04:18 PM)Yoriz Wrote: The lambda has been passed the return value of calling entry's get method so it will only ever contain that value which at that point will be empty as it has only just been created.
If you want to use lambda to get the value at the point of clicking the button you would need to call the get method.

Thank you Yoriz, that solved my problem.

(Jan-18-2022, 05:07 PM)menator01 Wrote: I didn't notice that you were using a function to call the register form so, here is a modified version example.

Thank you for your example! I will study your code.
Reply


Forum Jump:

User Panel Messages

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