Python - Tkinter - 图形用户界面

标签 python user-interface python-2.7 tkinter

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

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

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

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

  1. 用户单击选择颜色按钮:

    self.bc_bttn=Button(self, text='Select Color', command=lambda: self.callback())
    
  2. 该函数调用回调函数,我选择正确的颜色

    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. 我按下保存按钮

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

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

    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/

相关文章:

c++ - 在 QThread 中运行长时间操作并向主线程发送信号仍然会卡住 UI

java - 如何让 JLabel 和 JTextArea 对齐到屏幕的左上角而不是中心

python - 存在缺失值时对所有可能的组进行自定义分组

python - 类型错误 : 'numpy.ndarray' object is not callable within for loop Python

delphi - 用户 GUI Activity 如何破坏我的串行端口输入

python - 使用 python 日志记录模块将消息信息发送到一个文件并将错误发送到另一个文件

python - pygame Mixer 将音频保存到磁盘?

python - 隐藏python代码

python - 正则表达式中字符周围不需要的空格

python - 如何对大型数据集进行分组