【问题标题】:main function call fails in pythonpython中的main函数调用失败
【发布时间】:2013-07-27 17:23:29
【问题描述】:

我编写了以下代码来创建布局并在布局中显示一些文本。接下来,使用 urwid 库中的原始显示模块将布局显示在控制台屏幕上。 但是运行代码失败,因为它没有找到我在类中单独编写的函数 ma​​in

运行时的错误代码是:

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

【问题讨论】:

  • 你的函数没有返回任何东西,类声明在哪里?

标签: python user-interface python-2.7 console-application urwid


【解决方案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()

【讨论】:

  • 如果我的 sn-p 帮助了你,你能接受或支持我的回答吗?谢谢?如果您仍有问题,我们很乐意为您提供帮助!
  • 我已经编辑了我的代码(在构造函数和 main 中添加了一些缺失的 self.)。
  • 我还是遇到了一些问题。我无法将修改后的代码放在这里,所以我在这里开始了另一个问题:[stackoverflow.com/questions/17901909/…你在哪里为 ui 做声明?
  • 请注意我所做的更改“ui” => “self.ui”,这意味着我正在设置FormDisplay当前实例的“ui”属性(大致相当于“这个”在java中)。
  • 我正在尝试将我修改后的代码放在这里,但没有任何运气。以刻度开始,但无法换行。如何在注释中正确缩进我的代码? :-(
【解决方案2】:

我没有看到任何单独的课程。但根据您的代码,FormDisplay 是一个不返回任何内容的函数。 FormDisplay().main() 等于 None.main()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2023-02-06
  • 2022-12-15
相关资源
最近更新 更多