[Urwid] Enable/disable buttons?

Yasir Assam mail at endlessvoid.com
Mon Dec 7 15:20:17 EST 2009


Thanks for replying Ian.

I tried something like this yesterday, but I couldn't move the cursor 
past the OK button (I'm using the up/down keys to move the cursor and 
ignoring the mouse for now). Disabling the OK button puts the enabled 
Cancel out of each unless I use the mouse.

Here's the code:

import urwid

class DisabledButton(urwid.Button):

    def __init__(self, text):
        self.__super.__init__(text)

    def selectable(self):
        return False

f1 = urwid.Edit("Field 1:                      ")
f2 = urwid.Edit("Field 2:                      ")
f3 = urwid.Edit("Field 3:                      ")

#ok = urwid.Button("OK")
ok = DisabledButton("OK")
cancel = urwid.Button("Cancel")

gf = urwid.GridFlow([ok, cancel], 10, 10, 0, "center")

pile = urwid.Pile([
        f1,
        f2,
        f3,
        urwid.Divider(),
        gf
        ])

fill = urwid.Filler(pile, valign="top")

loop = urwid.MainLoop(fill)
loop.run()


On a related point - is it easy to make the tab key cycle the selected 
widget?

Thanks,
Yasir
> Yasir Assam wrote:
>   
>> ok = urwid.Button("OK")
>>     
> [...]
>   
>> I want to be able to disable the OK button depending on what's being
>> input. By "disable" I stopping the cursor from moving it to it when I
>> use the arrow keys to move focus around.
>>
>> Is there an easy way to do this?
>>     
>
> Yes, the selectable() method on widgets determines whether the cursor
> will move to a widget, so you could make a Button subclass that returns
> False from selectable() when the button should not be selectable.
>
> You will also have to intercept mouse clicks for it to be a complete
> solution.  Look at the code for the Button class, it's not very complicated.
>
> http://excess.org/urwid/browser/urwid/wimp.py#L412
>
> Ian
>   
-------------- next part --------------

No virus found in this outgoing message.
Checked by AVG - www.avg.com 
Version: 8.5.426 / Virus Database: 270.14.96/2549 - Release Date: 12/06/09 19:37:00


More information about the Urwid mailing list