1

code below 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. (More info on my complete project can be gleaned from questions at widget advice for a console project and urwid for a console project. My skype help request being here.) However running the code fails as an Assertion Error is raised as described below :

Error on running code is :
Traceback (most recent call last):
File "./yamlUrwidUIPhase6.py", line 98, in <module>
main()
File "./yamlUrwidUIPhase6.py", line 92, in main
form.main()
File "./yamlUrwidUIPhase6.py", line 48, in main
self.view = formLayout()
File "./yamlUrwidUIPhase6.py", line 77, in formLayout
ui.draw_screen(dim, frame.render(dim, True))
File "/usr/lib64/python2.7/site-packages/urwid/raw_display.py", line 535, in draw_screen
assert self._started
AssertionError

The code :

import sys  
sys.path.append('./lib')  
import os  
from pprint import pprint  
import random  
import urwid  
ui=urwid.raw_display.Screen()


class FormDisplay(object):

    def __init__(self):
        global ui
        self.ui = ui
        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):
        global ui
        #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()

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


def formLayout():
    global ui
    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))
    return

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 main():
    form = FormDisplay()
    form.main()

########################################
##### MAIN ENTRY POINT
########################################
if __name__ == '__main__':
    main()

I don't want to change the function formLayout as I intend to add more to this basic code framework, where in another function will be added that repeatedly calls formLayout to keep updating the screen based on reading values from a yml file. I already have a separate code that deals with reading the yaml file and extracting ordered dictionaries out it. After figuring out how to get basic urwid console working, I can move on to integrating both to create my final application.

4

2 回答 2

3

这意味着 self._started 已被评估为 False。

assert <Some boolean expression>, "Message if exp is False"

如果表达式被评估为 True,则不会发生任何特殊情况,但如果表达式被评估为 False,则会抛出 AssertionError 异常。

如果您想要更清晰的消息,您可以尝试/排除该行:

try:
    assert self._started, "The screen is not up and running !"
except AssertionError as e:
    print "Oops, something happened: " + str(e)

一些文档:http ://www.tutorialspoint.com/python/assertions_in_python.htm

于 2013-07-28T00:37:15.250 回答
2

你在屏幕formLayout前打电话。调用,这要求屏幕已经启动。startformLayoutui.draw_screen

于 2013-07-28T00:47:30.050 回答