Changing state of buttons.
Eric Brunel
eric_brunel at despammed.com
Fri Sep 10 03:54:49 EDT 2004
Jamey Saunders wrote:
> Hi all,
>
> I'm just learning Python (I'm about 8 hours in so far), and I have a
> problem. I'm writing a small Windows app using Tkinter. I have two
> buttons on my screen that I want to start in an inactive state
> (already have that working) and when two input fields have some data
> input, the buttons would become active. What's the best way to go
> about changing the state of buttons that already exist?
>
> Thanks!
I'm not sure I understand the meaning of your question: if it's really about
changing the state of an existing widget, a simple
widget.configure(state=NORMAL) will do what you want.
But the tricky part of what you want to do does not seem to be there: doing an
action whenever the text of an entry changes is a bit tricky with Tkinter. Here
is an example:
----------------------------------------------------------
from Tkinter import *
root = Tk()
## Create the entry and associated variable
v = StringVar()
e = Entry(root, textvariable=v)
e.pack(side=TOP)
## Create the button
b = Button(root, text='OK', command=root.quit, state=DISABLED)
b.pack(side=TOP)
## Function making the button active if needed
def makeBActive(*args):
## If there is something in the entry, activate the button
if v.get().strip():
b.configure(state=NORMAL)
## Otherwise de-activate it
else:
b.configure(state=DISABLED)
## Whenever a key is released in the entry, call the function above
e.bind('<KeyRelease>', makeBActive)
root.mainloop()
----------------------------------------------------------
HTH
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list