1

I wrote the following code that creates a layout and displays some text in the layout. Next the layout is displayed on the console screen using raw display module from urwid library. However running the code fails as it doesn't find the function main that I wrote separately in a class.

Error code on running is :

Traceback (most recent call last):  
  File "./yamlUrwidUIPhase5.py", line 89, in <module>
    main()  
  File "./yamlUrwidUIPhase5.py", line 83, in main
    FormDisplay().main()  
AttributeError: 'NoneType' object has no attribute 'main'

The code :

import sys  
sys.path.append('./lib')  
import os  
from pprint import pprint  
import random  
import urwid  

def formLayout():
    text1 = urwid.Text("Urwid 3DS Application program - F8 exits.")
    text2 = urwid.Text("One mission accomplished")

    textH = urwid.Text("topmost Pile text")
    cols = urwid.Columns([text1,text2])
    pile = urwid.Pile([textH,cols])
    fill = urwid.Filler(pile)

    textT  = urwid.Text("Display") 

    textSH = urwid.Text("Pile text in Frame")
    textF = urwid.Text("Good progress !")

    frame = urwid.Frame(fill,header=urwid.Pile([textT,textSH]),footer=textF)
    dim = ui.get_cols_rows()

    ui.draw_screen(dim, frame.render(dim, True))  

def RandomColor():
    '''Pick a random color for the main menu text'''
    listOfColors = ['dark red', 'dark green', 'brown', 'dark blue',
                    'dark magenta', 'dark cyan', 'light gray',
                    'dark gray', 'light red', 'light green', 'yellow',
                    'light blue', 'light magenta', 'light cyan', 'default']
    color = listOfColors[random.randint(0, 14)]
    return color

def FormDisplay():
    import urwid.raw_display
    ui = urwid.raw_display.Screen()
    palette = ui.register_palette([
            ('Field', 'dark green, bold', 'black'), # information fields, Search: etc.
            ('Info', 'dark green', 'black'), # information in fields
            ('Bg', 'black', 'black'), # screen background
            ('InfoFooterText', 'white', 'dark blue'), # footer text
            ('InfoFooterHotkey', 'dark cyan, bold', 'dark blue'), # hotkeys in footer text
            ('InfoFooter', 'black', 'dark blue'),  # footer background
            ('InfoHeaderText', 'white, bold', 'dark blue'), # header text
            ('InfoHeader', 'black', 'dark blue'), # header background
            ('BigText', RandomColor(), 'black'), # main menu banner text
            ('GeneralInfo', 'brown', 'black'), # main menu text
            ('LastModifiedField', 'dark cyan, bold', 'black'), # Last modified:
            ('LastModifiedDate', 'dark cyan', 'black'), # info in Last modified:
            ('PopupMessageText', 'black', 'dark cyan'), # popup message text
            ('PopupMessageBg', 'black', 'dark cyan'), # popup message background
            ('SearchBoxHeaderText', 'light gray, bold', 'dark cyan'), # field names in the search box
            ('SearchBoxHeaderBg', 'black', 'dark cyan'), # field name background in the search box
            ('OnFocusBg', 'white', 'dark magenta') # background when a widget is focused
           ])
urwid.set_encoding('utf8')

def main(self):
    #self.view = ui.run_wrapper(formLayout)
    self.view = formLayout()
    ui.start()
    self.loop = urwid.MainLoop(self.view, self.palette, unhandled_input=self.unhandled_input)
    self.loop.run()

def unhandled_input(self, key):
    if key == 'f8':
        quit()
        return
def main():
    global ui

    FormDisplay().main()

########################################
##### MAIN ENTRY POINT
########################################
if __name__ == '__main__':
    main()
falsetru
  • 357,413
  • 63
  • 732
  • 636
tauseef_CuriousGuy
  • 770
  • 1
  • 12
  • 28

2 Answers2

2

FormDisplay in your code is not a class but a function, something like this would be more appropriate. Btw, I'd recommend reading up on some of this doc http://docs.python.org/2/tutorial/classes.html

import urwid.raw_display

class FormDisplay(object):

    def __init__(self):
        self.ui = urwid.raw_display.Screen()
        self.palette = ui.register_palette([
        ('Field', 'dark green, bold', 'black'), # information fields, Search: etc.
        ('Info', 'dark green', 'black'), # information in fields
        ('Bg', 'black', 'black'), # screen background
        ('InfoFooterText', 'white', 'dark blue'), # footer text
        ('InfoFooterHotkey', 'dark cyan, bold', 'dark blue'), # hotkeys in footer text
        ('InfoFooter', 'black', 'dark blue'),  # footer background
        ('InfoHeaderText', 'white, bold', 'dark blue'), # header text
        ('InfoHeader', 'black', 'dark blue'), # header background
        ('BigText', RandomColor(), 'black'), # main menu banner text
        ('GeneralInfo', 'brown', 'black'), # main menu text
        ('LastModifiedField', 'dark cyan, bold', 'black'), # Last modified:
        ('LastModifiedDate', 'dark cyan', 'black'), # info in Last modified:
        ('PopupMessageText', 'black', 'dark cyan'), # popup message text
        ('PopupMessageBg', 'black', 'dark cyan'), # popup message background
        ('SearchBoxHeaderText', 'light gray, bold', 'dark cyan'), # field names in the search box
        ('SearchBoxHeaderBg', 'black', 'dark cyan'), # field name background in the search box
        ('OnFocusBg', 'white', 'dark magenta') # background when a widget is focused
       ])

    def main(self):
        #self.view = ui.run_wrapper(formLayout)
        self.view = formLayout()
        self.ui.start()
        self.loop = urwid.MainLoop(self.view, self.palette,   unhandled_input=self.unhandled_input)
        self.loop.run()

if __name__ == "__main__":
    form = FormDisplay()
    form.main()
Yuri
  • 4,254
  • 1
  • 29
  • 46
Ketouem
  • 3,820
  • 1
  • 19
  • 29
  • if my snippet has helped you, could you accept or upvote my answer thanks ? If you've still got an issue, we'll be happy to help you ! – Ketouem Jul 27 '13 at 21:10
  • I've edited my code (adding some missing self. in the constructor and in main). – Ketouem Jul 27 '13 at 21:11
  • i still got some issue. i was unable to put my modified code here so i started another question in here : [http://stackoverflow.com/questions/17901909/global-variable-in-main-not-getting-recognised-in-another-function-in-python] Where did you do the declaration for ui? – tauseef_CuriousGuy Jul 27 '13 at 21:23
  • Please note the change I've made "ui" => "self.ui", it means that I'm setting the "ui" attribute of the current instance of FormDisplay (roughly equivalent to "this" in java). – Ketouem Jul 27 '13 at 21:29
  • i am trying to put my modified code here without any luck. started with ticks but unable to put line breaks. how can i properly indent my code while putting it in comment ? :-( – tauseef_CuriousGuy Jul 27 '13 at 21:41
  • About that, I have the habit of inheriting of object to create a "new class" have a look on http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html but it is not what's blocking you here. – Ketouem Jul 27 '13 at 21:46
  • I don't think you can put code in the comment, just add an "Edit" paragraph in your original post. – Ketouem Jul 27 '13 at 21:49
  • well, that's new to me. but great that you pointed that out to me. :-) – tauseef_CuriousGuy Jul 27 '13 at 21:53
  • okay will put the current unworking code at the edit section of the other link where this question is - [http://stackoverflow.com/questions/17901909/global-variable-in-main-not-getting-recognised-in-another-function-in-python] – tauseef_CuriousGuy Jul 27 '13 at 21:56
0

I don't see any separate class. But according to your code, FormDisplay is a function that return nothing. FormDisplay().main() equals None.main().

zhangyangyu
  • 8,520
  • 2
  • 33
  • 43
  • FormDisplay is was not a class as you rightly pointed out. It is now declared a class but now I have issues with declaration and use of global ui. More here [http://stackoverflow.com/questions/17901909/global-variable-in-main-not-getting-recognised-in-another-function-in-python] – tauseef_CuriousGuy Jul 27 '13 at 21:47
  • new issue of attribute error at [http://stackoverflow.com/questions/17908588/attributeerror-while-trying-to-create-a-console-screen-using-urwid] – tauseef_CuriousGuy Jul 28 '13 at 13:25