【问题标题】:Python - Tkinter - GUIPython - Tkinter - 图形用户界面
【发布时间】:2013-04-25 18:57:35
【问题描述】:

我正在使用 Python 为我当前的项目创建一个新应用程序。这是我第一次使用它,这是一次学习体验......

我的应用程序中有一个按钮,它从 Python 调用 askcolor() 函数。第一次一切正常,但之后,它给了我以下错误。

AttributeError: 'str' object has no attribute 'set'

这是我在我的应用程序中工作的顺序:

  1. 用户点击Select Color按钮:

    self.bc_bttn=Button(self, text='Select Color', command=lambda: self.callback())
    
  2. 函数调用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)
    
  3. self.overlayColorValue.set(triple_bkgColor) 更改文本字段条目的值,以便用户在应用程序中看到正确的值

  4. 我按下Save 按钮

    self.overlayColorValue = self.bc_ent.get()
    body.set('overlay-color', self.overlayColorValue)
    
  5. 我的更改已写入 xml 文件

    tree.write(CONFIG_XML)
    
  6. 这次一切正常,但如果我想再次做同样的事情来改变颜色。然后我点击Select Color按钮时出现以下错误

    AttributeError: 'str' object has no attribute 'set'
    

【问题讨论】:

  • 我尝试修复格式。您应该验证代码中的缩进并在需要时对其进行修复。此外,尝试提出一个更具描述性的标题。当前的更像是一个标签列表,我添加了真正的标签。按edit 改进您的问题。

标签: python user-interface python-2.7 tkinter


【解决方案1】:

您将self.overlayColorValue 属性替换为self.bc_ent.get() 的返回值,即str

大概在那之前,它是一个标签,而你想在它上面调用.set()

self.overlayColorValue.set(self.bc_ent.get())
body.set('overlay-color', self.overlayColorValue.get())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-16
    • 2021-11-19
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多