2013-07-27 52 views
1

我編寫了下面的代碼來創建佈局並在佈局中顯示一些文本。接下來,使用urwid庫中的原始顯示模塊在控制檯屏幕上顯示佈局。 但是運行代碼失敗,因爲它找不到我在課堂上單獨編寫的功能主要。在運行python中的主函數調用失敗

錯誤代碼是:

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' 

代碼:

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() 
+0

你的功能不返回任何東西的閱讀了,哪來的類的聲明? –

回答

1

你的代碼中的FormDisplay不是一個類,而是一個函數,像這樣的東西會更合適。順便說一句,我建議一些這個文檔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() 
+0

如果我的代碼段幫助了你,你能接受還是upvote我的回答謝謝?如果您仍然遇到問題,我們很樂意爲您提供幫助! – Ketouem

+0

我已經編輯了我的代碼(在構造函數和main中添加了一些缺失的self。)。 – Ketouem

+0

我還是有一些問題。我無法把我的修改代碼放在這裏,所以我開始在這裏的另一個問題:[http://stackoverflow.com/questions/17901909/global-variable-in-main-not-getting-recognised-in-another-function- in-python]你在哪裏做ui的聲明? –

0

我看不出有任何單獨的類。但根據你的代碼,FormDisplay是一個不返回任何內容的函數。 FormDisplay().main()等於None.main()

+0

FormDisplay不是您正確指出的類。現在它被宣佈爲一個班級,但現在我有關於全球UI的聲明和使用的問題。更多這裏[http://stackoverflow.com/questions/17901909/global-variable-in-main-not-getting-recognised-in-another-function-in-python] –

+0

新問題屬性錯誤[http:///stackoverflow.com/questions/17908588/attributeerror-while-trying-to-create-a-console-screen-using-urwid] –