Python 它说我的函数未定义,它已定义

Python 它说我的函数未定义,它已定义,python,tkinter,pycharm,Python,Tkinter,Pycharm,我有一门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代码:

我有一门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

小错误,更改:

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


请注意,从普通函数调用更改为在类中调用同级方法(
self.fromHexDex
而不是
fromHexDex

不确定这是否是格式问题,但如果这是实际代码,则类为空:D

除此之外:尝试通过“self”调用函数。这条线将是:

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

command=lambda:self.fromHexDec(self.fromHex.get())
使用self和lambda lambda的意思是什么?它在runtime@zsrkmyn它起作用了!谢谢发布一个答案,这样我就可以“声明”它作为答案使用
lambda
而不仅仅是
self.fromHexDec(self.fromHex.get())
的好处是什么。。。您可以从HEXDEC调用self.fromHexDec,并将其结果分配给
命令
参数。仅供我理解。。。我的解决方案和@putonspectacles的解决方案有什么区别?我只是好奇,因为我还在学习。tk.Button想要一个可调用的对象,该对象接受0个参数,它将在以后调用,但您调用了该对象并将其结果分配给
回调
参数。您将在上找到一个很好的解释,特别是“将参数传递给回调”部分。好的,谢谢:)
self.dec = tk.Button(self, height=1, width=6, text="Decimal", command=self.fromHexDec(self.fromHex.get()))