Skip to content Skip to sidebar Skip to footer

Tkinter -how To Enable A Button State To 'normal' From Disabled When User Starts Typing Inside Entry Field?

I am using tkinter to build a GUI. I have two field, One Entry field for accepting user input and other is submit button. My intention is when the GUI invokes Submit button should

Solution 1:

You can use Variable class objects in sync with entry's text, the button is disabled as long as the entry is empty, and enabled when not in the example below:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk


def toggle_state(*_):
    if entry.var.get():
        button['state'] = 'normal'
    else:
        button['state'] = 'disabled'


if __name__ == '__main__':
    root = tk.Tk()
    entry = tk.Entry(root)
    entry.var = tk.StringVar()
    entry['textvariable'] = entry.var
    entry.var.trace_add('write', toggle_state)
    button = tk.Button(root, text="Button", state='disabled')
    entry.pack()
    button.pack()
    tk.mainloop()

Post a Comment for "Tkinter -how To Enable A Button State To 'normal' From Disabled When User Starts Typing Inside Entry Field?"