python中的函数无法识别_在python的另一个函数中无法识别main中的全局变量

本文档描述了在Python程序中遇到的一个问题,即在`main`函数中声明的全局变量`ui`在另一个函数中无法识别。错误信息显示`NameError: global name 'ui' is not defined`。问题出在尝试在`FormDisplay`类的初始化方法中使用未定义的`ui`变量。为了解决这个问题,`ui`应该在`main`函数之前进行初始化,确保它在需要的地方可见。
摘要由CSDN通过智能技术生成

下面的代码创建一个布局并在布局中显示一些文本。接下来,使用urwid库中的原始显示模块在控制台屏幕上显示布局。但是,运行代码失败,因为在main中声明的全局变量ui在另一个函数中无法识别。在

运行时的错误代码是:

Traceback (most recent call last):

File "./yamlUrwidUIPhase6.py", line 97, in

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文件读取值来不断更新屏幕。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值