0

私はいくつかのPythonコードを実行していますが、エラーが発生します:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "multiline14.py", line 28, in getText
    if encodinghex in process:
TypeError: argument of type 'function' is not iterable

私のdefは以下です。

def gui(item):
    def default_encode(s):
        pass

    # map items from menu to commands
    encodinghex = '.encode("hex")'
    decodinghex = '.decode("hex")'
    mapping = {"encode_b64": base64.encodestring,"encode_url": urllib.quote_plus,"encode_hex": encodinghex, "decode_b64": base64.decodestring, "decode_url": urllib.unquote_plus, "decode_hex": decodinghex}


    process = mapping.get(item, default_encode)

    def getText():
    #clear bottom text field
        bottomtext.delete(1.0, END)
    #var equals whats in middle
        var = middletext.get(1.0, 'end-1c')

    #insert encoded var in bottom
        if encodinghex in process:
            var = '"%s"' % (var)
            bottomtext.insert(INSERT, eval(var + process))
        elif decodinghex in process:
            var = '"%s"' % (var)
            bottomtext.insert(INSERT, eval(var + process))
        else:
            bottomtext.insert(INSERT, process(var))

そのエラーの原因は何ですか?

4

3 に答える 3

1

あなたがしたことはまったく意味がないようです。eval実行するコードに変換するために使用する 2 つのテキスト文字列、encodinghex とデコーディング hex があります。しかし、mappingそれらと並んであなたの辞書には、あなたが渡そうとしているさまざまな実際のメソッドもありますeval-それ自体は失敗するはずですが、その前でも、コードは既存のテキスト文字列を実際の関数値、これは不可能です。

于 2012-07-24T10:38:28.103 に答える
1

ここから関数をリクエストしてmappingいます:

process = mapping.get(item, default_encode)

次に、ここでそれを繰り返します。

if encodinghex in process:

in件名が でない限り、キーワードを使用することはできませんIterable

ここで達成しようとしているのは、呼び出しがどのmapping.get()関数を返したかを実際に確認することです

if process == encodinghex:

base64.encodestring, urllib.quote_plus, encodinghex, base64.decodestring, urllib.unquote_plus, decodinghexすべての関数であることに注意してください

于 2012-07-24T11:05:11.647 に答える
0

サンプルの最後の行から判断すると、process()どこかで呼び出された関数があります。それでも、行内の iterable であるかのようにアクセスしようとしますif encodinghex in process。エラーを修正するには、関数または iterable の名前を変更します。

于 2012-07-24T10:35:58.167 に答える