如何使用 tkinter 更改按钮颜色 [英] How to change button color with tkinter
本文介绍了如何使用 tkinter 更改按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不断收到以下错误:AttributeError: 'NoneType' 对象没有属性 'configure'
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")
推荐答案
当您执行 self.button = Button(...).grid(...)
时,会分配给 self.button
是 grid()
命令的结果,不是对创建的 Button
对象的引用.
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.
您需要在打包/网格化之前分配您的 self.button
变量.它应该看起来像这样:
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屋!
查看全文