Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Changing state of buttons.

Reply
Thread Tools

Changing state of buttons.

 
 
Jamey Saunders
Guest
Posts: n/a
 
      09-09-2004
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!
 
Reply With Quote
 
 
 
 
Brian Szmyd
Guest
Posts: n/a
 
      09-10-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!


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
 
Reply With Quote
 
 
 
 
Eric Brunel
Guest
Posts: n/a
 
      09-10-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

 
Reply With Quote
 
Jamey Saunders
Guest
Posts: n/a
 
      09-10-2004
Brian Szmyd <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Jamey Saunders
Guest
Posts: n/a
 
      09-10-2004
Eric Brunel <(E-Mail Removed)> wrote in message news:<chrm5f$6bc$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer'. Mike Larkin ASP .Net 1 05-23-2005 12:33 PM
Unable to make the session state request to the session state server Not Liking Dot Net Today ASP .Net 0 04-21-2004 11:54 AM
What is the state of state machine after power-up without reset conditions Weng Tianxiang VHDL 7 11-25-2003 06:24 PM
unable to make the session state request to the session state server shamanthakamani ASP .Net 1 11-20-2003 04:51 AM
State machine: how to stay in a state? David Lamb VHDL 1 09-15-2003 05:24 PM



Advertisments