python - 'AttributeError' while using a ListBox in urwid -
code, using urwid library, below supposed display contents of yml file (as given here) on console screen (as per application form layout here :
code :
import sys sys.path.append('./lib') import os pprint import pprint import random import urwid ui = urwid.raw_display.screen() pile = urwid.pile([]) collections import ordereddict import yaml_ordered_dict import collections import yaml import itertools import copy class formdisplay(object): def __init__(self): global ui #self.ui = urwid.raw_display.screen() 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 search box ('searchboxheaderbg', 'black', 'dark cyan'), # field name background in search box ('onfocusbg', 'white', 'dark magenta') # background when widget focused ]) urwid.set_encoding('utf8') def main(self): global ui #self.view = ui.run_wrapper(formlayout) #urwid initialisation first or yml readin first. chose yml reading yordict = yaml_ordered_dict.load('applyform.yml') yordictlevel1 = yordict.get('applyform') self.ui.start() self.view = formlayout(yordictlevel1) self.loop = urwid.mainloop(self.view, self.palette) self.loop.widget = self.view self.loop.run() def find_value(needle, container): # found object. return. if isinstance(container, basestring) , needle in container: return true values = none if isinstance(container, dict): values = container.values() elif hasattr(container, '__iter__'): values = container.__iter__() if values none: return false # check deeper in container, if needed. val in values: if find_value(needle, val): return true # no match found. return false def getchildmap(tmpmap): tmap = tmpmap tmplen = len(tmap) listofkeys = tmap.keys() topkey = listofkeys[0] #print('topkey', topkey) mapobtained = tmap.get(topkey) #print('getchildmap',mapobtained) return mapobtained def getmapforkeyindex(tmpmap,index): listofkeysformap = tmpmap.keys() = index mapobtained = collections.ordereddict(itertools.islice(tmpmap.items(), i, i+1)) #print(type(mapobtained)) #print("getmapforkeyindex: key , map",i,mapobtained) return mapobtained def checkvalofkey(key,tmpmap): tmap = tmpmap k, v in tmap.items(): if k == key : tval = tmap[key] #print('checkvalofkey:tval',tval) return tval def removetopitem(tmpmap): tmap = tmpmap listofkeys = tmap.keys() topkey = listofkeys[0] del tmap[topkey] return tmap def createblock(maptoworkwith) : global pile #pile = urwid.pile([]) tmaptoworkwith = copy.deepcopy(maptoworkwith) tmaptoworkwith = getchildmap(tmaptoworkwith) #print('createblock: tmaptoworkwith', tmaptoworkwith) reducedmaptocarry = copy.deepcopy(tmaptoworkwith) listofkeys = tmaptoworkwith.keys() #if blockpresentasvalueinmap(tmaptoworkwith) == true : if checkvalofkey('$type',tmaptoworkwith) == 'block' : #create block nd pile print('xxxxxxxxcreate block , pile listxxxxxxxx') #pile = urwid.pile([]) keytobeginpile = 2 #if displaypresentaskeyinmap(tmaptoworkwith) == true : #if checkcontainmentofkeyinmap('$display',(tmaptoworkwith)) : reducedmaptocarry = removetopitem(reducedmaptocarry) reducedmaptocarry = removetopitem(reducedmaptocarry) if (tmaptoworkwith.has_key('$display')) == true : print('display value ',tmaptoworkwith['$display']) displaytext = tmaptoworkwith['$display'] text = urwid.text(('generalinfo', displaytext), align='center') pile.contents.append(text) keytobeginpile +=1 #adddisplaytopile reducedmaptocarry = removetopitem(reducedmaptocarry) k in range(keytobeginpile,len(tmaptoworkwith)) : #//get value of key @ indexkeytobeginpile in tmaptoworkwith tmpmap = tmaptoworkwith.get(listofkeys[k]) if checkvalofkey('$type',tmpmap) != 'block' : print('##add pile##') print('display value ',tmpmap['$display']) displaytext = tmpmap['$display'] text = urwid.text(('generalinfo', displaytext), align='center') #pile.contents.append(text,options='pack') pile.append(text) keytobeginpile +=1 reducedmaptocarry = removetopitem(reducedmaptocarry) #print('for loop traversal: map', reducedmaptocarry) #maptopass(pop) continue else : createblock(reducedmaptocarry) #return urwid.linebox(urwid.attrwrap(urwid.overlay(urwid.linebox(urwid.padding( # pile, align='center', left=3, right=3)), banner, 'center', 150, 'middle', none), # 'generalinfo')) def formlayout(topmap): global ui global pile f3 = open('orderedmapinfinalapp.yaml',"w") newmap = topmap #check if needed @ yaml.dump(newmap, f3) content = urwid.simplelistwalker([]) listbox = urwid.listbox(content) #orderedmap has come #read key value , start loop print('lenght of map',len(newmap)) in range(len(newmap)): maptoworkwith = getmapforkeyindex(newmap,i) #print('for loop nd maptoworkwith is',i,maptoworkwith) if == 0 : if (checkvalofkey('$type',maptoworkwith) == 'block') : print('1st key read') continue else : if (maptoworkwith.has_key('title') == true): print('2nd key read') continue else : pile = ([]) createblock(maptoworkwith) #content.append(addblocktooveralllayout()) content.append(pile) continue fill = urwid.filler(listbox) frame = urwid.frame(fill,header=urwid.pile([textt,textsh]),footer=textf) dim = ui.get_cols_rows() #ui treated global handle functions, either belonging #to class or standalone functions such formlayout #need check if screen has been started if not ui._started: print("screen has not been started, no use of rendering.thus return :-( ") return #ui.draw_screen(dim, listbox.render(dim, true)) return frame #return listbox def randomcolor(): '''pick random color 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(): #global ui form = formdisplay() form.main() ######################################## ##### main entry point ######################################## if __name__ == '__main__': main() #
the code use ui
represent console screen. have used global listbox api contain values, yml file, key $display. code fails giving error below :
traceback (most recent call last): file "./yamlurwiduiphase8framefinalappc.py", line 308, in <module> main() file "./yamlurwiduiphase8framefinalappc.py", line 302, in main form.main() file "./yamlurwiduiphase8framefinalappc.py", line 61, in main self.loop.run() file "/home/gehna/urwidworkinprogress/urwid/main_loop.py", line 272, in run self.screen.run_wrapper(self._run) file "/home/gehna/urwidworkinprogress/urwid/raw_display.py", line 242, in run_wrapper return fn() file "/home/gehna/urwidworkinprogress/urwid/main_loop.py", line 312, in _run self.draw_screen() file "/home/gehna/urwidworkinprogress/urwid/main_loop.py", line 563, in draw_screen canvas = self._topmost_widget.render(self.screen_size, focus=true) file "/home/gehna/urwidworkinprogress/urwid/widget.py", line 141, in cached_render canv = fn(self, size, focus=focus) file "/home/gehna/urwidworkinprogress/urwid/container.py", line 1058, in render focus , self.focus_part == 'body') file "/home/gehna/urwidworkinprogress/urwid/widget.py", line 141, in cached_render canv = fn(self, size, focus=focus) file "/home/gehna/urwidworkinprogress/urwid/decoration.py", line 811, in render top, bottom = self.filler_values(size, focus) file "/home/gehna/urwidworkinprogress/urwid/decoration.py", line 796, in filler_values height = self._original_widget.rows((maxcol,),focus=focus) attributeerror: 'listbox' object has no attribute 'rows'
i checked parsing section of code ddd , pydb debugger , parsing yml section seems okay , print statements give values of $display correctly. there wrong way using ui ? should use draw_screen instead ?
strange, code should work is. had same error when creating frames , giving them wrong order of arguments, best guess change line
frame = urwid.frame(fill,header=urwid.pile([textt,textsh]),footer=textf)
to
frame = urwid.frame(fill,urwid.pile([textt,textsh]))
or
frame = urwid.frame(header=urwid.pile([textt,textsh]),fill)
discard footer temporarily until figure out right order of parameters inside frame , put back.
Comments
Post a Comment