在标签上使用 .config() 时 python 中的错误

Eri*_*les -1 python tkinter python-2.7

好的,所以我有问题。我正在尝试制作一个 GUI 十六进制转换器,但我一直收到同样的错误。我对 Tkinter 没有经验,所以有人可以帮助我吗?这是代码:

from Tkinter import *

def getNum():
   hex1 = e1.get()
   dec1 = int(hex1, 16)
   ol.configure(text=dec1)



root = Tk()

introLabel = Label(root, text='Input Hex Code: ').pack()

e1 = Entry(root)
e1.pack()
e1.focus_set()

inputButton = Button(root, text='Submit Hex Code', command=getNum).pack()

ol = Label(root, text='Hex Code will appear here.').pack()



root.mainloop()
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:/Users/The Lodges/Desktop/Python/test.py", line 6, in getNum
ol.configure(text=dec1)
AttributeError: 'NoneType' object has no attribute 'configure'
Run Code Online (Sandbox Code Playgroud)

mat*_*yce 5

来自的返回值.pack()不是小部件,而是None.

从此更改代码:

ol = Label(root, text='Hex Code will appear here.').pack()
Run Code Online (Sandbox Code Playgroud)

对此:

ol = Label(root, text='Hex Code will appear here.')
ol.pack()
Run Code Online (Sandbox Code Playgroud)

这将保持ol“指向”标签。