Reputation: 69
So I am trying to make the button enabled when text is entered in entry widget using tkinter however I don't think of an idea for it to work. My code is:
def capture():
if e.get():
button['state'] = 'normal'
e = Entry(root, font = 20,borderwidth=5,command = capture)
e.pack()
However I do know that Entry widget has no parameter called command.
Upvotes: 1
Views: 807
Reputation: 15098
One of the ways to achieve this is using StringVar
:
def capture(*args):
if e.get():
button['state'] = 'normal'
else:
button['state'] = 'disabled'
var = StringVar()
e = Entry(root, font = 20,borderwidth=5,textvariable=var)
e.pack()
var.trace('w',capture)
trace()
will call the provided callback each time the value of var
is changed.
The second way is to use bind
:
def capture():
if e.get():
button['state'] = 'normal'
else:
button['state'] = 'disabled'
e = Entry(root, font = 20,borderwidth=5)
e.pack()
e.bind('<KeyRelease>',lambda e: capture()) # Bind keyrelease to the function
With bind
, each time the key is released the function is called, whatever the key maybe. This might be a bit better because you are not creating a StringVar
just for the sake of using its trace
.
Upvotes: 2