Python - Tkinter - GUI [英] Python - Tkinter - GUI

查看:46
本文介绍了Python - Tkinter - GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a new application for my current project using Python. This is the first time I use it and it has been a learning experience...

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

I have a button in my application that calls the askcolor() function from Python. Everything works fine the first time but after that, it gives me the following error.

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

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

This is the sequence that I have working in my application:

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

self.bc_bttn=Button(self, text='Select Color', command=lambda: self.callback())

  • 函数调用了callback函数,我选择了合适的颜色

  • The function calls the callback function and I select the proper color

    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.set(triple_bkgColor) changes the value of the text field entry so the user will see the correct value on the application

    我按下保存按钮

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

  • 我的更改已写入 xml 文件

  • My changes are written to the xml file

    tree.write(CONFIG_XML)
    

  • 这次一切正常,但如果我想再次做同样的事情来改变颜色.然后当我单击 Select Color 按钮

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

  • 推荐答案

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

    You replaced your self.overlayColorValue attribute with the return value of self.bc_ent.get(), which is a str.

    据推测,在那之前,它是一个标签,而您想在其上调用 .set() :

    Presumably, before that time, it was a label, and you wanted to call .set() on it instead:

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

    这篇关于Python - Tkinter - GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆