如何使用tkinter更改按钮颜色

发布于 2021-01-29 18:22:09

我不断收到以下错误:AttributeError:’NoneType’对象没有属性’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")
关注者
0
被浏览
168
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    执行时self.button = Button(...).grid(...),分配给self.buttongrid()命令的结果是命令的结果,
    而不是Button创建的对象的引用。

    您需要self.button在打包/网格化之前分配变量。它看起来应该像这样:

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看