代码创建一个布局并在布局中显示一些文本。接下来,使用urwid库中的原始显示模块在控制台屏幕上显示布局。但是,运行代码失败,因为在main中声明的全局变量ui在另一个函数中无法识别。
运行时的错误代码是:
Traceback (most recent call last):
File "./yamlUrwidUIPhase6.py", line 97, in <module>
main()
File "./yamlUrwidUIPhase6.py", line 90, in main
form = FormDisplay()
File "./yamlUrwidUIPhase6.py", line 23, in __init__
palette = ui.register_palette([
NameError: global name 'ui' is not defined
代码:
import sys
sys.path.append('./lib')
import os
from pprint import pprint
import random
import urwid
global ui
class FormDisplay(object):
def __init__(self):
self.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()
self.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 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))
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文件中的值来更新屏幕。
答案 0 :(得分:1)
在global ui
中编写main
除了使main
本身可以写入名为ui
的全局变量之外什么都不做。如果要从其他函数(例如__init__
)写入该全局变量,则还需要在其中包含global
声明。要分配给全局变量的每个函数都必须有自己的global
声明。
答案 1 :(得分:0)
好的,感谢各位人士提供的各种建议,帮助我了解全球范围。我跟着Jeff Shannon的回答:Using global variables in a function other than the one that created them似乎能够摆脱使用全局ui引起的名称范围错误。这是我所做的,我在导入后使用ui = urwid.raw_display.Screen(),然后在使用它的函数中声明了全局ui。但现在我得到了一种不同的错误。最好用现在获得的错误开始一个新问题。