我正在使用 Python 为当前项目创建一个新应用程序。这是我第一次使用它,这是一次学习经历......
我的应用程序中有一个按钮,可以从 Python 调用 askcolor()
函数。第一次一切正常,但之后,它给了我以下错误。
AttributeError: 'str' object has no attribute 'set'
这是我在应用程序中工作的顺序:
用户单击
选择颜色
按钮:self.bc_bttn=Button(self, text='Select Color', command=lambda: self.callback())
该函数调用
回调
函数,我选择正确的颜色def callback(self): (triple, hexstr) = askcolor() if triple: triple_string = str(triple) triple_string2 = re.findall('[0-9, ]',triple_string); triple_bkgColor = ''.join(triple_string2) print triple_bkgColor self.overlayColorValue.set(triple_bkgColor)
self.overlayColorValue.set(triple_bkgColor)
更改文本字段条目的值,以便用户在应用程序上看到正确的值我按下
保存
按钮self.overlayColorValue = self.bc_ent.get() body.set('overlay-color', self.overlayColorValue)
我的更改已写入 xml 文件
tree.write(CONFIG_XML)
这次一切正常,但如果我想再次做同样的事情来改变颜色。然后当我单击
选择颜色
按钮时出现以下错误AttributeError: 'str' object has no attribute 'set'
最佳答案
您将 self.overlayColorValue
属性替换为 self.bc_ent.get()
的返回值,该值是一个 str
。
大概,在那之前,它是一个标签,而您想对其调用 .set()
:
self.overlayColorValue.set(self.bc_ent.get())
body.set('overlay-color', self.overlayColorValue.get())
关于Python - Tkinter - 图形用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16222618/