如何更改与Tkinter的按钮颜色 [英] How to change button color with tkinter

查看:257
本文介绍了如何更改与Tkinter的按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到以下错误:
AttributeError异常:'NoneType'对象有没有属性'配置'

 #创建颜色按钮
self.button =按钮(个体经营,
                     文字=点击我,
                     命令= self.color_change,
                     BG =蓝
                    ).grid(行= 2,列= 2,粘= W)高清color_change(个体经营):
    更改按钮的颜色    self.button.configure(BG =红)


解决方案

当你做 self.button =按钮(...)。格(...)什么被分配到 self.button 格的结果()命令的按钮参考对象创建的。

您需要打包/ griding之前,您 self.button 变量分配。
它应该是这个样子:

  self.button =按钮(个体经营,文本=点击我,命令= self.color_change,BG =蓝)
self.button.grid(行= 2,列= 2,粘= W)

I keep getting the following error: AttributeError: 'NoneType' object has no attribute 'configure'

# create color button
self.button = Button(self,
                     text = "Click Me",
                     command = self.color_change,
                     bg = "blue"
                    ).grid(row = 2, column = 2, sticky = W)

def color_change(self):
    """Changes the button's color"""

    self.button.configure(bg = "red")

解决方案

When you do self.button = Button(...).grid(...), what gets assigned to self.button is the result of the grid() command, not a reference to the Button object created.

You need to assign your self.button variable before packing/griding it. It should look something like this:

self.button = Button(self,text="Click Me",command=self.color_change,bg="blue")
self.button.grid(row = 2, column = 2, sticky = W)

这篇关于如何更改与Tkinter的按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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