Changing state of buttons.

J

Jamey Saunders

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!
 
B

Brian Szmyd

Jamey said:
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!

Hi Jamey,

First off, many people will agree when I say ditch Tkinter, and switch to
wxPython. Never the less...

Not knowing a lot of Tkinter I would guess you can initialize these buttons
to be in the "greyed out" state, and create an event on the two input
fields that checks for any data input. Make the event call some validation
function if you need it, and change the state of the button to "non-greyed
out" if all is well!

-regards
brian szmyd
 
E

Eric Brunel

Jamey said:
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()
 
J

Jamey Saunders

Brian Szmyd said:
Hi Jamey,

First off, many people will agree when I say ditch Tkinter, and switch to
wxPython. Never the less...

Not knowing a lot of Tkinter I would guess you can initialize these buttons
to be in the "greyed out" state, and create an event on the two input
fields that checks for any data input. Make the event call some validation
function if you need it, and change the state of the button to "non-greyed
out" if all is well!

-regards
brian szmyd

Hi Brian,

Thanks for the recommendation on wxPython. I'll check it out. It's
not like I've lost a ton of time learning Tkinter. That does bring up
the question of why is wxPython better than Tkinter, though.

The basic idea you recommend is exactly what I want to do, but the
problem I'm having is actually changing the buttons' state. I have
them initialized to inactive, and I'm checking the input fields for
data. My problem is that I don't know (and can't find an example
anywhere) how to change the state of a button that has already been
packed.

I'll check out wxPython, though. It may be that I'm just using the
wrong GUI extensions...

Thanks again,

Jamey Saunders
 
J

Jamey Saunders

Eric Brunel said:
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.

Thanks, Eric. That's just what I was after. I'm thinking that this
may be easier if I use wxPython. I've been checking it out this
morning.

Thanks again!

Jamey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top