全局变量错误,尽管在每个函数中将变量声明为全局变量

时间:2012-06-23 23:08:10

标签: python textbox global-variables

我正在尝试使用全局变量。我已经将它声明为全局,并在每次提及时声明它,但在第一个函数完成后我得到一个NameError。这是代码,我想我已经疯了但我似乎无法找到问题。

def on_servername_insertatcursor(self, widget):
    global output  
    output = StringIO.StringIO()         
    servername = widget.get_text()
    output.write("USHARE_NAME="+servername+'\n')

def on_netif_changed(self, widget):
    netif = widget.get_active_text()
    global output
    output.write("USHARE_IFACE="+netif+'\n')

def on_port_insertatcursor(self, widget):
    global output
    port = widget.get_text()
    output.write("USHARE_PORT="+port+'\n')

def on_telprt_insertatcursor(self, widget):
    global output
    telprt = widget.get_text()
    output.write("USHARE_TELNET_PORT="+telprt+'\n')

def on_dirs_insertatcursor(self, widget):
    global output
    dirs = widget.get_text()
    output.write("USHARE_DIR="+dirs+'\n')

def on_iconv_toggled(self, widget):
    global output
    iconv = widget.get_active()
    if iconv == True:
        output.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
    else:
        output.write("USHARE_OVERRIDE_ICONV_ERR="+"False"+'\n')

def on_webif_toggled(self, widget):
    global output
    webif = widget.get_active()
    if webif == True:
        output.write("USHARE_ENABLE_WEB="+"yes"+'\n')
    else:
        output.write("USHARE_ENABLE_WEB="+"no"+'\n')

def on_telif_toggled(self, widget):
    global output
    telif = widget.get_active()
    if telif == True:
        output.write("USHARE_ENABLE_TELNET="+"yes"+'\n')
    else:
        output.write("USHARE_ENABLE_TELNET="+"no"+'\n')

def on_xbox_toggled(self, widget):
    global output
    xbox = widget.get_active()
    if xbox == True:
        output.write("USHARE_ENABLE_XBOX="+"yes"+'\n')
    else:
        output.write("USHARE_ENABLE_XBOX="+"no"+'\n')

def on_dlna_toggled(self, widget):
    global output
    dlna = widget.get_active()
    if dlna == True:
        output.write("USHARE_ENABLE_DLNA="+"yes"+'\n')
    else:
        output.write("USHARE_ENABLE_DLNA="+"no"+'\n')

def on_commit_clicked(self, widget):
    commit = output.getvalue()
    logfile = open('/home/boywithaxe/Desktop/ushare.conf','w')
    logfile.write(commit)

def on_endprogram_clicked(self, widget):
    sys.exit(0)

令人惊奇的是,当insertatcursor(来自Gtk.TextBuffer.insert_at_cursor())被替换为activate时,代码工作正常,除了我不希望用户在每次输入数据后都必须按Enter键。 / p>

EDIT。回溯如下

Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line     58, in on_netif_changed
output.write("USHARE_IFACE="+netif+'\n')
NameError: global name 'output' is not defined

做了@jdi建议的更改(谢谢顺便说一下,我看到了背后的逻辑),我得到的Traceback如下:

Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 55, in on_netif_changed
OUTPUT.write("USHARE_IFACE="+netif+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 72, in on_iconv_toggled
OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 79, in on_webif_toggled
OUTPUT.write("USHARE_ENABLE_WEB="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 86, in on_telif_toggled
OUTPUT.write("USHARE_ENABLE_TELNET="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 93, in on_xbox_toggled
OUTPUT.write("USHARE_ENABLE_XBOX="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 100, in on_dlna_toggled
OUTPUT.write("USHARE_ENABLE_DLNA="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 105, in on_commit_clicked
commit = OUTPUT.getvalue()
NameError: global name 'OUTPUT' is not defined

3 个答案:

答案 0 :(得分:1)

您的代码示例不需要全局。只需删除它。您需要在函数中使用global关键字的唯一时间是您要分配给它。

OUTPUT = StringIO.StringIO()         

def on_servername_insertatcursor(self, widget):
    servername = widget.get_text()
    OUTPUT.write("USHARE_NAME="+servername+'\n')

def on_netif_changed(self, widget):
    netif = widget.get_active_text()
    OUTPUT.write("USHARE_IFACE="+netif+'\n')

def on_port_insertatcursor(self, widget):
    port = widget.get_text()
    OUTPUT.write("USHARE_PORT="+port+'\n')

def on_telprt_insertatcursor(self, widget):
    telprt = widget.get_text()
    OUTPUT.write("USHARE_TELNET_PORT="+telprt+'\n')

def on_dirs_insertatcursor(self, widget):
    dirs = widget.get_text()
    OUTPUT.write("USHARE_DIR="+dirs+'\n')

def on_iconv_toggled(self, widget):
    iconv = widget.get_active()
    if iconv == True:
        OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
    else:
        OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"False"+'\n')

即使您希望能够在函数中重置StringIO对象,它仍然不需要赋值或全局关键字:

def reset_output(self):
    OUTPUT.seek(0)
    OUTPUT.truncate()

证明其有效

import StringIO

OUTPUT = StringIO.StringIO()         

def foo():
    OUTPUT.write('foo')

def bar():
    OUTPUT.write('bar')

def print_output():
    print OUTPUT.getvalue()

def reset_output():
    OUTPUT.seek(0)
    OUTPUT.truncate()

if __name__ == "__main__":
    foo()
    bar()
    print_output()
    reset_output()
    print_output()

输出

$ python test.py 
foobar

$

答案 1 :(得分:0)

尝试将output = StringIO.StringIO()移到文件外的所有功能之外。

答案 2 :(得分:0)

嗯,问题的问题是不幸的缩进:)看PyGTK docs表明显示的on_...函数确实是类的方法,而不是全局函数,所以“全局”变量是可能不是真正的全局,但也是类的成员(只需查看方法定义中的self参数)。

我在askubuntu上给出了更详细的答案,这是一个代码片段,显示了需要做的事情:

class MyApp(gtk.Window):

    output = None

    def __init__(...):
        ...
        self.output = StringIO.StringIO()

    def on_servername_insertatcursor(self, widget):    
        servername = widget.get_text()
        self.output.write("USHARE_NAME="+servername+'\n')

    def on_netif_changed(self, widget):
        netif = widget.get_active_text()
        self.output.write("USHARE_IFACE="+netif+'\n')

绝对没有涉及PyGTK特定或信号特定的魔法:)