尝试使用urwid创建控制台屏幕时出现“ AttributeError” [英] 'AttributeError' while trying to create a console screen using urwid

查看:90
本文介绍了尝试使用urwid创建控制台屏幕时出现“ AttributeError”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码创建布局,并在布局中显示一些文本。接下来,使用urwid库中的原始显示模块在控制台屏幕上显示布局。 (有关完整项目的更多信息,请参见有关小工具的建议控制台项目针对控制台项目的urbd 。我的Skype帮助请求位于此处。)但是运行代码失败,因为如下所述引发了AttributeError。在/usr/lib64/python2.7/site-packages/urwid上查看urwid的源代码时,我看到main_loop.py和curses_display.py都具有带有不同参数的draw_screen函数:
main_loop.py-> def draw_screen(self):

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 AttributeError is raised as described below. On looking at source code for urwid at /usr/lib64/python2.7/site-packages/urwid I see that both main_loop.py and curses_display.py have a draw_screen function with different arguments : main_loop.py -> def draw_screen(self):

curses_display.py> def draw_screen(self,(cols,rows),r):

curses_display.py > def draw_screen(self, (cols, rows), r ):

我是否必须指定要通过从specificFile导入draw_screen 命令之类的东西使用哪个?还是我在看画布概念有根本上的错误?我还可以看到文件
/usr/lib64/python2.7/site-packages/urwid/ 容器中的框架类( Class Frame(BoxWidget):)。 py 具有渲染功能( def render(self,size,focus = False):

Do I have to specify which one to use by something like import draw_screen from specificFile command ? Or is there anything fundamentally wrong I am looking at the canvas concept ? I can also see that the frame class (class Frame(BoxWidget): ) in the file /usr/lib64/python2.7/site-packages/urwid/container.py has a render function (def render(self, size, focus=False):)

运行代码错误是:


追溯(最近一次调用是最近的):

文件 ./yamlUrwidUIPhase6.py,第104行,在< module>中

main()
文件 ./yamlUrwidUIPhase6.py,第98行,位于main

form.main()

文件中。 /yamlUrwidUIPhase6.py,第51行,位于main

self.loop.run()

文件 /usr/lib64/python2.7/site-packages/urwid/main_loop中.py,第274行,在运行中

self.screen.run_wrapper(self._run)

文件 /usr/lib64/python2.7/site-packages/urwid/ raw_display.py,第237行,在run_wrapper
中,返回fn()

文件 /usr/lib64/python2.7/site-packages/urwid/main_loop.py,第285行,在_r un
self.draw_screen()

文件 /usr/lib64/python2.7/site-packages/urwid/main_loop.py,行508,在draw_screen
$ b $中b canvas = self._topmost_widget.render(self.screen_size,focus = True)

AttributeError:'NoneType'对象没有属性'render'

代码:

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
        self.palette = self.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.ui.start()
        self.view = formLayout()

        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 is treated as global handle for all functions, either belonging
    #to any class or standalone functions such as formLayout
    #need to check if screen has been started
    if not ui._started:
        print("Screen has not been started, so no use of rendering.Thus return :-( ")
        return

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

我不想更改formLayout函数,因为我打算在此基本代码框架中添加更多内容,其中将在另一个函数中添加以下内容:反复调用formLayout以根据从yml文件读取的值来不断更新屏幕。我已经有一个单独的代码来处理yaml文件并提取有序的字典。在弄清楚如何使基本的urwid控制台正常工作之后,我可以继续进行集成以创建最终应用程序。

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.

推荐答案

通过在代码中添加以下行(如此问题

The Attribute Error was removed by adding following lines in the code as shown at this question.

第1行:类FormDisplay

line 1 : self.loop.widget = self.view in main of class FormDisplay

第2行:
执行返回框而不是返回在函数formLayout()

line 2 : doing a return Frame instead of return in the function formLayout()

第3行中:
unhandled_input 函数中添加了行来处理按键

line 3 : added lines to handle key presses in unhandled_input function

这篇关于尝试使用urwid创建控制台屏幕时出现“ AttributeError”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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