python中主函数调用失败

2024-05-23 22:15:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

运行时的错误代码是:

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'

代码:

^{pr2}$

Tags: 模块函数代码inpy文本屏幕main
2条回答

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

你的代码中的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()

相关问题 更多 >