Changing state of buttons.

Discussion in 'Python' started by Jamey Saunders, Sep 9, 2004.

  1. 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!
    Jamey Saunders, Sep 9, 2004
    #1
    1. Advertising

  2. Jamey Saunders

    Brian Szmyd Guest

    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
    Brian Szmyd, Sep 10, 2004
    #2
    1. Advertising

  3. Jamey Saunders

    Eric Brunel Guest

    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
    Eric Brunel, Sep 10, 2004
    #3
  4. Brian Szmyd <> wrote in message news:<>...
    > 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
    Jamey Saunders, Sep 10, 2004
    #4
  5. Eric Brunel <> wrote in message news:<chrm5f$6bc$>...
    > 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
    Jamey Saunders, Sep 10, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger
    Replies:
    1
    Views:
    611
    GreggTB
    May 20, 2005
  2. =?Utf-8?B?QmVu?=

    Image Buttons/Buttons not responding

    =?Utf-8?B?QmVu?=, Jul 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    432
    Teemu Keiski
    Jul 6, 2005
  3. Jeremy Jones

    Datagrid Nav buttons and numeric buttons

    Jeremy Jones, Mar 21, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    600
    Masudur
    Mar 22, 2007
  4. Jonathan N. Little
    Replies:
    3
    Views:
    511
    Andy Dingley
    Apr 2, 2007
  5. Dr. Leff
    Replies:
    3
    Views:
    279
    David Mark
    Oct 15, 2007
Loading...

Share This Page