在Label上使用.config()时python出错

时间:2014-09-27 18:01:05

标签: python python-2.7 tkinter

好的,我有一个问题。我正在尝试制作一个GUI Hex转换器并且我继续得到相同的错误。我对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()

我一直收到这个错误:

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'

1 个答案:

答案 0 :(得分:3)

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

更改此代码:

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

对此:

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

这将使ol'指向'标签。