我有这个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 :(得分: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
)
答案 1 :(得分:0)
不确定这是否是格式化问题,但如果这是实际代码,则您的类为空:D
除此之外:尝试通过“self”调用该函数。该行将是:
self.dec = tk.Button(self, height=1, width=6, text="Decimal", command=self.fromHexDec(self.fromHex.get()))