我是python的新手,无法让测试程序正常运行。目标是使用对象来创建一个地址簿来保存信息。小部件运行但是当单击按钮而不是更新时我得到错误
"Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\neuvillerl\Desktop\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
File "C:\Users\neuvillerl\workspace\vtk_testing\wid.py", line 71, in <lambda>
btns = Tkinter.Button(nam, text = x.get_name(), command= lambda x=x: update(x),bg = 'blue', fg = 'white')
File "C:\Users\neuvillerl\workspace\vtk_testing\wid.py", line 61, in update
lbl5.configure(nam, text = "name: " + str(person.get_name()))
File "C:\Users\neuvillerl\Desktop\Python27\lib\lib-tk\Tkinter.py", line 1331, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\neuvillerl\Desktop\Python27\lib\lib-tk\Tkinter.py", line 1322, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: unknown option "-colormap""
代码:
nam = Tkinter.Tk()
class person(object):
def __init__(self, name, phone, address, email):
self.name = name
self.phone = phone
self.address = address
self.email = email
def get_name(self):
return self.name
def get_phone(self):
return self.phone
def get_address(self):
return self.address
def get_email(self):
return self.email
riley = person("Riley", "XXX-XXX-XXXX", "This place", "email1@hi.com")
alex = person("Alex","XXX-XXX-XXXX", "That place", "email2@hi.com")
maggie = person("Maggie","XXX-XXX-XXXX","Other place","email3@hi.com")
lst = [riley,alex,maggie]
def update(person):
lbl5.configure(nam, text = "name: " + str(person.get_name()))
lbl6.configure(nam, text = "phone#: " + str(person.get_phone()))
lbl7.configure(nam, text = "address: " + str(person.get_address()))
lbl8.configure(nam, text = "email: " + str(person.get_email()))
lbl5 = Tkinter.Label(nam, text = "name:" )
lbl6 = Tkinter.Label(nam, text = "phone#:" )
lbl7 = Tkinter.Label(nam, text = "address:" )
lbl8 = Tkinter.Label(nam, text = "email:" )
for x in lst:
btns = Tkinter.Button(nam, text = x.get_name(), command= lambda x=x:
update(x),bg = 'blue', fg = 'white')
btns.pack(fill = Tkinter.X)
lbl5.pack()
lbl6.pack()
lbl7.pack()
lbl8.pack()
nam.mainloop()`
答案 0 :(得分:2)
问题是您将nam
实例作为第一个参数传递给Label.configure
方法。你的功能应该是:
def update(person):
lbl5.configure(text = "name: " + str(person.get_name()))
lbl6.configure(text = "phone#: " + str(person.get_phone()))
lbl7.configure(text = "address: " + str(person.get_address()))
lbl8.configure(text = "email: " + str(person.get_email()))