0

我正在尝试使用全局变量。我已经将它声明为全局,并在每次提及时都声明它,但是在第一个函数完成后我得到一个 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() )被替换为激活时,代码运行良好,除了我不希望用户在每次输入数据后都必须按 enter。

编辑。Traceback 如下

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
4

3 回答 3

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

$
于 2012-06-23T23:15:31.493 回答
0

尝试移出output = StringIO.StringIO()文件中的所有函数。

于 2012-06-23T23:12:20.150 回答
0

好吧,这个问题的问题是不幸的缩进 :) 查看PyGTK 文档表明显示的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 特定或信号特定的魔法:)

于 2012-06-24T22:28:14.337 回答