将鼠标悬停在 tkinter 中的按钮颜色上

hover over button color in tkinter

我正在处理这个 tkinter 项目,我快要完成了,但是当我将鼠标悬停在按钮上时,我似乎找不到更改按钮颜色的方法所以你能帮忙吗这是我的代码

import tkinter as tk

window = tk.Tk()
img = tk.PhotoImage(file='C:\Users\laithmaree\PycharmProjects\create_apps_with_python\brainicon.ico.png')
window.title("Quiz Game")

# i created an icon
# i made a title


window.geometry("800x600")
window.resizable(width=False, height=False)
window.iconphoto(False, img)

label1 = tk.Label(window, text='Quiz App', font=("Arial Bold", 25))
label1.pack()

txtbox = tk.Entry(window, width=50)


def playbuttonclicked():
    label1.destroy()
    playbtn.destroy()
    quitbtn.destroy()
    label2 = tk.Label(window, text='What is the short form of computer science', font=("Arial Bold", 25))
    label2.pack()
    txtbox.place(x=250, y=200, height=40)

    def chkanswer():
        useranswer = txtbox.get()  # Get contents from Entry
        if useranswer == 'cs':
            lblcorrect = tk.Label(window, text='correct')
            lblcorrect.pack()

            def delete():
                lblcorrect.destroy()

            lblcorrect.after(1000, delete)


        else:
            lblwrong = tk.Label(window, text='Try Again')
            lblwrong.pack()

            def deletefunction():
                lblwrong.destroy()

            lblwrong.after(1000, deletefunction)

    submitbtn = tk.Button(window, text='Submit', font=('Arial Bold', 30), command=chkanswer, bg='red')
    submitbtn.place(x=305, y=400)


playbtn = tk.Button(window, text='Play', font=("Arial Bold", 90), bg='red', command=playbuttonclicked)
playbtn.place(x=10, y=200)


def quitbuttonclicked():
    window.destroy()


quitbtn = tk.Button(window, text='Quit', font=("Arial Bold", 90), bg='red', command=quitbuttonclicked)
quitbtn.place(x=400, y=200)

window.mainloop()

这些按钮是 submitbtn、playbtn、quitbtn 我希望悬停在按钮上的按钮是黑色的,那里已经是红色的我只是希望当我将鼠标悬停在按钮上时它们是黑色的thx

将每个按钮绑定到进入和离开事件(当鼠标进入和离开小部件时),并根据哪个按钮被触发,更改按钮的背景颜色:

btn.bind('<Enter>', lambda e: e.widget.config(bg='black'))
btn.bind('<Leave>', lambda e: e.widget.config(bg='red'))