Home > Software engineering >  Lock tk button if required information is missing
Lock tk button if required information is missing

Time:10-20

I'm currently creating a GUI and I want to lock a "continue"-button, if information, like an email-adress, is not entered yet. Thanks to anyone who's trying to help :)

CodePudding user response:

Use .trace() for your StringVars for each entry field. You can call a function when the user begins typing into an entry field.

Here's an example:

import tkinter as tk

root = tk.Tk()
root.geometry("200x150")

def update(*args):
    if name_var.get() == '' or email_var.get() == '':
        cont['state'] = tk.DISABLED
    else:
        cont['state'] = tk.NORMAL

name_lbl = tk.Label(root, text="Name:").pack()
name_var = tk.StringVar(root)
name_var.trace("w", update)
name = tk.Entry(root, textvariable=name_var).pack()

email_lbl = tk.Label(root, text="Email:").pack()
email_var = tk.StringVar(root)
email_var.trace("w", update)
email = tk.Entry(root, textvariable=email_var).pack()

cont = tk.Button(root, state=tk.DISABLED, text="Continue")
cont.pack(side=tk.BOTTOM)

root.mainloop()
  • Related