Python 3.x 如何通过类中的绑定使按钮更改颜色

Python 3.x 如何通过类中的绑定使按钮更改颜色,python-3.x,tkinter,colors,mouseevent,Python 3.x,Tkinter,Colors,Mouseevent,我尝试使用绑定来绑定鼠标单击,以根据按钮的前景和背景更改颜色 from tkinter import * class Clicks(): def __init__(self, master): frame=Frame(master) frame.pack() #trying to bind the mouse clicks to change the color of the button self.button1=

我尝试使用绑定来绑定鼠标单击,以根据按钮的前景和背景更改颜色

from tkinter import *


class Clicks():
    def __init__(self, master):
        frame=Frame(master)
        frame.pack()

        #trying to bind the mouse clicks to change the color of the button

        self.button1= Button(frame, text="Click Me!", fg='red', bg='black')

        self.button1.bind("<Button-1>", fg='black')
        self.button1.bind("<Button-3>", bg='red')
        self.button1.grid(row = 0, column = 1, sticky = W)


root = Tk()
b = Clicks(root)
root.mainloop()
从tkinter导入*
类单击():
定义初始(自我,主):
帧=帧(主帧)
frame.pack()
#尝试绑定鼠标单击以更改按钮的颜色
self.button1=按钮(框,text=“单击我!”,fg='red',bg='black')
self.button1.bind(“,fg='black')
self.button1.bind(“,bg='red')
self.button1.grid(行=0,列=1,粘性=W)
root=Tk()
b=点击次数(根)
root.mainloop()

TypeError:bind()得到一个意外的关键字参数“fg”

请检查代码段。这里可以使用两种方法

首先,您可以使用
lambda
函数
bind

from tkinter import *
class Clicks():
    def __init__(self, master):
        frame=Frame(master)
        frame.pack()
        self.button1= Button(frame, text="Click Me!", fg='red', bg='black')
        self.button1.bind("<Button-1>", lambda event, b=self.button1: b.configure(bg="green",fg="blue"))
        self.button1.grid(row = 0, column = 1, sticky = W)
root = Tk()
b = Clicks(root)
root.mainloop()

请检查代码片段。这里可以使用两种方法

首先,您可以使用
lambda
函数
bind

from tkinter import *
class Clicks():
    def __init__(self, master):
        frame=Frame(master)
        frame.pack()
        self.button1= Button(frame, text="Click Me!", fg='red', bg='black')
        self.button1.bind("<Button-1>", lambda event, b=self.button1: b.configure(bg="green",fg="blue"))
        self.button1.grid(row = 0, column = 1, sticky = W)
root = Tk()
b = Clicks(root)
root.mainloop()