主函数调用在python中失败 [英] main function call fails in python

查看:119
本文介绍了主函数调用在python中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



错误代码正在运行的是:

 跟踪(最近一次通话为最后一次):
文件 ./yamlUrwidUIPhase5.py,行89,在< module>中
main()
文件 ./yamlUrwidUIPhase5.py,第83行,位于主
FormDisplay()。main()
AttributeError中:'NoneType'对象没有属性' main'

代码:

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

def formLayout():
text1 = urwid.Text( Urwid 3DS应用程序-F8退出。)
text2 = urwid.Text(完成一项任务)

textH = urwid.Text(最上面的桩文本)
cols = urwid.Columns([text1,text2])
桩= urwid.Pile([textH ,cols])
fill = urwid.Filler(pile)

textT = urwid.Text( Display)

textSH = urwid.Text( Pile框架中的文本)
textF = urwid.Text(进度很好!)

框架= 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():
'''为主菜单文本选择随机颜色'''
listOfColors = [深红色,深绿色,棕色,深蓝色,
深洋红色,深青色,浅灰色,
深灰色, '浅红色','浅绿色','黄色',
'浅蓝色','浅品红色','浅青色','默认']
color = listOfColors [random.randint(0 ,14)]
返回颜色

def FormDisplay():
import urwid.raw_display
ui = urwid.raw_display.Screen()
调色板= ui.register_palette([
('Field','dark green,bold','black'),#信息字段,搜索:等。
('Info','dark green','black '),#字段
('Bg','black','black')中的信息,#屏幕背景
('InfoFooterText','white','深蓝色'),#页脚文本
('InfoFooterHotkey','dark cy an,粗体,深蓝色),#页脚文本
中的热键( InfoFooter,黑色,深蓝色),#页脚背景
( InfoHeaderText,白色,粗体,深蓝色),#标头文本
( InfoHeader,黑色,深蓝),#标头背景
( BigText,RandomColor(),黑色 ),#主菜单标题文字
('GeneralInfo','brown','black'),#主菜单标题
('LastModifiedField','深青色,粗体','黑色'), #上次修改:
('LastModifiedDate','深青色','黑色'),#上次修改中的信息:
('PopupMessageText','black','深青色'),#弹出消息文本
( PopupMessageBg,黑色,深青色),#弹出消息背景
( SearchBoxHeaderText,浅灰色,粗体,深青色),#字段名在搜索框
('SearchBoxHeaderBg','black','dark cyan')中,#在搜索框
('OnFo cusBg','white','dark magenta')#小部件聚焦时的背景
])
urwid.set_encoding('utf8')

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

def unhandled_input(self,key):
如果key =='f8' :
quit()
return
def main():
全局ui

FormDisplay()。main()

####################################
#####主要入口点
#######################################
如果__name__ =='__main__':
main()


解决方案

FormDisplay不是一个类,而是一个函数,像这样的东西会更合适。顺便说一句,我建议您阅读一些此文档 http://docs.python。 org / 2 / tutorial / classes.html

  import urwid.raw_display 

类FormDisplay(object):

def __init __(self):
self.ui = urwid.raw_display.Screen()
self.palette = ui.register_palette([
(字段,深绿色,粗体,黑色),#个信息字段,搜索:等。
(信息,深绿色,黑色),#个字段中的信息
('Bg','black','black'),#屏幕背景
('InfoFooterText','white','深蓝色'),#页脚文字
('InfoFooterHotkey' ,'深青色,粗体','深蓝色'),#页脚文本
中的热键('InfoFooter','黑色','深蓝色'),#页脚背景
('InfoHeaderText', '白色,粗体','深蓝色'),#标题文本
('InfoHeader','黑色','深蓝色'),#标题背景
('BigText',RandomColor(),'black'),#主菜单标题文本
('GeneralInfo','brown','black'),#主菜单文本
('LastModifiedField', 'dark cyan,bold','black'),#上次修改:
('LastModifiedDate','dark cyan','black'),#上次修改的信息:
('PopupMessageText', 'black','dark cyan'),#弹出消息文本
('PopupMessageBg','black','dark cyan'),#弹出消息背景
('SearchBoxHeaderText','浅灰色,粗体,深青色),#搜索框
中的字段名( SearchBoxHeaderBg,黑色,深青色),#搜索框
中的字段名背景( OnFocusBg ','white','dark magenta')#小部件聚焦时的背景
])

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 = se lf.unhandled_input)
self.loop.run()

如果__name__ = __main__:
form = FormDisplay()
form.main()


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

解决方案

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

这篇关于主函数调用在python中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆