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() に由来する) が activate に置き換えられると、コードが完全に機能することです。ただし、ユーザーがデータを入力するたびに Enter キーを押す必要はありません。

編集。トレースバックは次のとおりです

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 (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 に答える