簡體   English   中英

根據用戶輸入更改 label 的顏色

[英]Change the color of a label based on user input

我使用 Tkinter 創建了一個歌曲猜測程序,我擁有它,因此用戶按下按鈕播放歌曲,他們輸入他們認為的歌曲,然后在聽完並猜測所有歌曲后提交。 現在在輸入框的右側,我有一個 label,它顯示文本“正確”或“不正確”,以確定他們是否把這首歌弄對了,有沒有辦法讓這個文本在他們得到它時變成綠色當他們弄錯時,正確和紅色。 我已經嘗試了下面代碼中列出的一些東西。

from playsound import playsound
import tkinter as tk
from tkinter import *

master=tk.Tk()
master.title("Song Guessing Game")
master.config(bg="ghost white")


def play1():
    playsound("sickomode.mp3")

def play2():
    playsound("onedance.mp3")

def play3():
    playsound("uptownfunk.mp3")

def play4():
    playsound("shapeofyou.mp3")

tk.Label(master,text="Song Guesser",font="Verdanda 20 underline bold",bg="ghost white",fg="black").grid(row=0,column=1)

tk.Button(master,text="Play Song 1",command=play1,font="Verdanda 15 bold").grid(row=1,column=0)
tk.Button(master,text="Play Song 2",command=play2,font="Verdanda 15 bold").grid(row=2,column=0)
tk.Button(master,text="Play Song 3",command=play3,font="Verdanda 15 bold").grid(row=3,column=0)
tk.Button(master,text="Play Song 4",command=play4,font="Verdanda 15 bold").grid(row=4,column=0)

guess1=tk.Entry(master,bg="ghost white",fg="black",font="Verdanda 15")
guess2=tk.Entry(master,bg="ghost white",fg="black",font="Verdanda 15")
guess3=tk.Entry(master,bg="ghost white",fg="black",font="Verdanda 15")
guess4=tk.Entry(master,bg="ghost white",fg="black",font="Verdanda 15")

guess1.grid(row=1,column=1)
guess2.grid(row=2,column=1)
guess3.grid(row=3,column=1)
guess4.grid(row=4,column=1)


def submit():
    s1guess=str(guess1.get()).upper()
    if s1guess=="SICKO MODE":
        print ("Correct")
        status.set("Correct")
        
    elif s1guess!="SICKO MODE":
        print("Incorrect")
        status.set("Incorrect")
    s2guess=str(guess2.get()).upper()
    if s2guess=="ONE DANCE":
        print("Correct")
        status2.set("Correct")
    elif s2guess!="ONE DANCE":
        print("Incorrect")
        status2.set("Incorrect")
    s3guess=str(guess3.get()).upper()
    if s3guess=="UPTOWN FUNK":
        print("Correct")
        status3.set("Correct")
    elif s3guess!="UPTOWN FUNK":
        print("Incorrect")
        status3.set("Incorrect")
    s4guess=str(guess4.get()).upper()
    if s4guess=="SHAPE OF YOU":
        print("Correct")
        status4.set("Correct")
    elif s4guess!="SHAPE OF YOU":
        print("Incorrect")
        status4.set("Incorrect")

status=StringVar()
status2=StringVar()
status3=StringVar()
status4=StringVar()


scolor=StringVar()
s2color=StringVar()
s3color=StringVar()
s4color=StringVar()


tk.Label(master,textvariable=status,font="Verdanda 15 bold",fg=scolor.get()).grid(row=1,column=2)
tk.Label(master,textvariable=status2,font="Verdanda 15 bold",fg=s2color.get()).grid(row=2,column=2)
tk.Label(master,textvariable=status3,font="Verdanda 15 bold",fg=s3color.get()).grid(row=3,column=2)
tk.Label(master,textvariable=status4,font="Verdanda 15 bold",fg=s4color.get()).grid(row=4,column=2)


tk.Button(master,text="Submit Score",command=submit).grid(row=5,column=1)


master.mainloop()

我正在查看的主要區域是底部的標簽集。 以我目前的方式,我已經設法讓它工作了,但是 scolor 變量的默認值只是 "" 這不是顏色,因此代碼不是 function

使用配置方法:

...
label.configure(foreground='[colour you want]')
...

您需要使用Label.config(fg=...)來更改Label的前景色,因此您需要將變量分配給結果標簽:

label1 = tk.Label(master, font="Verdanda 15 bold", width=10)
label2 = tk.Label(master, font="Verdanda 15 bold", width=10)
label3 = tk.Label(master, font="Verdanda 15 bold", width=10)
label4 = tk.Label(master, font="Verdanda 15 bold", width=10)

label1.grid(row=1, column=2)
label2.grid(row=2, column=2)
label3.grid(row=3, column=2)
label4.grid(row=4, column=2)

然后修改submit()以根據猜測結果更改這些標簽的前景色:

def submit():
    def check(guess, expected, label):
        if guess.get().strip().upper() == expected:
            label.config(text="Correct", fg="green")
        else:
            label.config(text="Incorrect", fg="red")

    check(guess1, "SICKO MODE", label1)
    check(guess2, "ONE DANCE", label2)
    check(guess3, "UPTOWN FUNK", label3)
    check(guess4, "SHAPE OF YOU", label4)

請注意,您根本不需要那些statusXsXcolor變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM