我正在處理這個 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
uj5u.com熱心網友回復:
將每個按鈕系結到進入和離開事件(當滑鼠進入和離開小部件時),并根據觸發的事件更改按鈕的背景顏色:
btn.bind('<Enter>', lambda e: e.widget.config(bg='black'))
btn.bind('<Leave>', lambda e: e.widget.config(bg='red'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/394477.html
上一篇:運行注冊應用程式時,我不斷收到NameError:name'root'isnotdefinedinTkinter
