2017-02-26 4 views
-2

私はこのTkinterクラスを持っていますが、これは完全に動作しますが、この部分はなぜ分かりませんか?ありがとう!私の関数は定義されておらず、定義されています

私はpycharmを使用しています。それが答えに

> Error: 

> self.dec = tk.Button(self, height=1, width=6, text="Decimal", command=fromHexDec(self.fromHex.get())) 
NameError: global name 'fromHexDec' is not defined 

Pythonコードを変更するかどうかわからない:

class Tkk(tk.Tk): 
    """"initiating the calculator""" 

    def __init__(self): 
     tk.Tk.__init__(self) 
     container = tk.Frame(self) 
     container.configure(bg="#eee", width=400, height=200) 
     container.pack(fill="both", expand=1, side="top") 
     self.label = tk.Label(self, text="Choose from one of bases to convert from below") 
     self.label.pack() 
     self.hexEnt = tk.Entry(self) 
     self.hex = tk.Button(self, height=1, width=9, text="Hexadecimal", command=self.hexa) 
     self.hexEnt.pack() 
     self.hex.pack() 
    def fromHexDec(self, num): 
     toDecimal(num, 16) 
    def hexa(self): 
     """"creating new variables""" 
     self.fromHex = tk.Entry(self) 
     self.bin = tk.Button(self, height=1, width=6, text="Binary") 
     self.oc = tk.Button(self, height=1, width=6, text="Octal") 
     self.dec = tk.Button(self, height=1, width=6, text="Decimal", command=fromHexDec(self.fromHex.get())) 

     self.label1 = tk.Label(self, text="You have chosen to convert from Hexa! Pick the base you want to convert to") 
     """"packing the variables""" 
     self.fromHex.pack() 
     self.label1.pack() 
     self.oc.pack() 
     self.dec.pack() 
     self.bin.pack() 
     """destroying the current variables""" 
     self.hex.destroy() 
     self.hexEnt.destroy() 
     self.label.destroy() 




frame = Tkk() 
frame.mainloop() 

注:

self.dec = tk.Button(self, height=1, width=6, text="Decimal", 
command=fromHexDec(self.fromHex.get())) 

へ:はそこまで

+0

'コマンド=ラムダ:self.fromHexDec(self.fromHex.get())'使用の自己とラムダ – abccd

+0

ラムダは何を意味するのでしょうか? – guy

+0

実行時に関数を作成します – abccd

答えて

1

小さな誤り、変更Tkinterの定義:

self.dec = tk.Button(self, height=1, width=6, text="Decimal", 
command=self.fromHexDec(self.fromHex.get())) 

予告クラスの兄弟メソッドを呼び出す通常の関数呼び出しからの変更(代わりにfromHexDexself.fromHexDex

0

これは、書式設定の問題ですが、かどうかわからない、これは実際のコードがある場合は、あなたのクラス空です:D

これ以外に "self"で関数を呼び出そうとします。行は次のようになります。

self.dec = tk.Button(self, height=1, width=6, text="Decimal", command=self.fromHexDec(self.fromHex.get())) 
+1

これはうまくいくでしょう...あなたは' self.fromHexDec'を呼び出し、その結果を 'command'引数に代入します。 – tdelaney

+0

私の理解のために...私の解決策と@putonspectaclesの違いは何ですか?私はまだ学んでいるので、私はちょっと面倒です。 – Simulacrum

+1

tk.Buttonは後で呼び出す0のパラメータを取る呼び出し可能オブジェクトを必要としますが、オブジェクトを呼び出してその結果を 'callback'パラメータに割り当てました。 [effbot](http://effbot.org/zone/tkinter-callbacks.htm) - 特に「コールバックへの引数の受け渡し」の項を参照してください。 – tdelaney

関連する問題