Sorry, I forgot to actually attach the file...

Hello,

I'm trying to implement a generic class for a dialog popup. I'm attaching the 
code for the implementation. I don't know the reason why it gives me an error 
at some point. To trigger the error one must:
- click on browse
- click on whatever OK or Cancel

Would someone please review the code and comment on it...

Thank you,
   Arcadie
#!/usr/bin/python
# -*- coding: utf-8 -*-

import urwid
from urwid.command_map import command_map

class DialogExit(Exception):
    pass


class DialogDisplay(urwid.WidgetWrap):
    palette = [
        ('body','black','white'),
        ('border','black','white'),
        ('shadow','white','black'),
        ('selectable','black', 'dark cyan'),
        ('focus','black','dark cyan','bold'),
        ('focustext','light gray','dark blue'),
        ('button normal','light gray', 'dark blue', 'standout'),
        ('button select','white',      'dark green'),
       ]
    parent = None
    def __init__(self, text, width, height, body=None, loop=None, parent=None):
        width = int(width)
        if width <= 0:
            width = ('relative', 80)
        height = int(height)
        if height <= 0:
            height = ('relative', 80)
    
        self.body = body
        if body is None:
            # fill space with nothing
            body = urwid.Filler(urwid.Divider(),'top')

        self.frame = urwid.Frame( body, focus_part='footer')
        if text is not None:
            self.frame.header = urwid.Pile( [urwid.Text(text),
                urwid.Divider(u'\u2550')] )
        w = self.frame
        
        # pad area around listbox
        w = urwid.Padding(w, ('fixed left',2), ('fixed right',2))
        w = urwid.Filler(w, ('fixed top',1), ('fixed bottom',1))
        w = urwid.AttrWrap(w, 'body')
        
        w = urwid.LineBox(w)
        
        # "shadow" effect
        w = urwid.Columns( [w,('fixed', 1, urwid.AttrWrap(
            urwid.Filler(urwid.Text(('border',' ')), "top")
            ,'shadow'))])
        w = urwid.Frame( w, footer = 
            urwid.AttrWrap(urwid.Text(('border',' ')),'shadow'))
        if parent is None:
            # outermost border area
            w = urwid.Padding(w, 'center', width )
            w = urwid.Filler(w, 'middle', height )
            w = urwid.AttrWrap( w, 'border' )
        else:
            self.parent = parent
            #w = urwid.Overlay(w, parent, 'center', width+2, 'middle', height+2)
        self.loop = loop
        self.view = w

    def add_buttons(self, buttons):
        l = []
        for name, exitcode in buttons:
            b = urwid.Button( name, self.button_press )
            b.exitcode = exitcode
            b = urwid.AttrWrap( b, 'button normal','button select' )
            l.append( b )
        self.buttons = urwid.GridFlow(l, 10, 3, 1, 'center')
        self.frame.footer = urwid.Pile( [ urwid.Divider(u'\u2500'),
            self.buttons ], focus_item = 1)

    def button_press(self, button):
        if self.parent is None:
            raise DialogExit(button.exitcode)
        else:
            self.loop.widget=self.parent

    def show(self):
        if self.loop is None:
            self.loop = urwid.MainLoop(self.view, self.palette)
            try:
                self.loop.run()
            except DialogExit, e:
                return self.on_exit( e.args[0] )
        else:
            self.loop.widget = self.view
        
    def on_exit(self, exitcode):
        return exitcode, ""
        


class MyCheckBox(urwid.CheckBox):
    def keypress(self, size, key):
        if (key == " ") and (command_map[key] == "activate"):
            self.toggle_state()
        else:
            return key

class AddToolDialog(DialogDisplay):
    def __init__(self, text, width, height, parent=None):
        body = self.dialog_body()
        self.loop = None
        DialogDisplay.__init__(self, text, width, height, body, parent)

    def on_browse_button(self, button):
        d = DialogDisplay( "Hello", 50, 10, None, self.loop, self )
#        d = DialogDisplay( "Hello", 50, 10)
        d.add_buttons([    ("OK", 0), ("Cancel", 1) ])
        d.show()
        return
        
    def button(self, caption, callback):
        w = urwid.Button(caption, callback)
        w = urwid.AttrWrap(w, 'button normal', 'button select')
        return w

    def dialog_body(self):
        tools_list = ["First tool", "Second tool", "Third tool"]
        self.widgets = []
        # Tool id input
        self.widgets += [urwid.Edit("Tool ID:")]
        # Tool name input
        self.widgets += [urwid.Edit("Name:")]
        # Tool description input
        self.widgets += [urwid.Edit("Description:")]
        # Tool dependancies list
        self.widgets += [urwid.Text("Dependancies:")]
        self.widgets += [MyCheckBox(tool) for tool in tools_list]
        # Configuration scripts
        self.widgets += [urwid.Text("Configuration scripts:")]
        self.widgets += [self.button("Browse", self.on_browse_button)]
        self.listbox = urwid.ListBox(urwid.SimpleListWalker([urwid.AttrWrap(w, None, 'reveal focus') for w in self.widgets]))
        return self.listbox

    def next_widget(self,input):
        if input == "enter":
            found = False
            while not found:
                self.listbox.set_focus(self.listbox.get_focus()[1] + 1)
                found = self.listbox.get_focus()[0].selectable()

    def show(self):
        self.loop = urwid.MainLoop(self.view, self.palette, unhandled_input = self.next_widget)
        try:
            self.loop.run()
        except DialogExit, e:
            return self.on_exit( e.args[0] )
        

def main():
    view = AddToolDialog("Add a new tool", 100, 20)
    view.add_buttons([    ("Quit", 0) ])
    exitcode, exitstring = view.show()
    
if '__main__'==__name__:
    main()
_______________________________________________
Urwid mailing list
[email protected]
http://lists.excess.org/mailman/listinfo/urwid

Reply via email to