2017-02-26 89 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() 

注:定义的Tkinter在那里

+0

'命令=拉姆达:self.fromHexDec(self.fromHex.get())'使用自我和lambda – abccd

+0

什么拉姆达是什么意思? – guy

+0

它在运行时创建一个函数 – abccd

回答

1

误差小,变化:

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

到:

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

通知从一个普通的函数调用来调用兄弟方法在一个类(self.fromHexDex而不是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

相关问题