它说我的函数是未定义的

2024-06-17 10:06:35 发布

您现在位置: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


Tags: thetotextselfcontainerdefbuttonwidth
2条回答

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

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

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

小错误,更改:

 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)的变化

相关问题 更多 >