根据用户输入更改标签的颜色

2024-10-01 00:35:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Tkinter创建了一个歌曲猜测程序,用户可以按一个按钮播放歌曲,输入他们认为歌曲是什么,然后在收听和猜测所有歌曲后,按submit。现在,在输入框的右侧,我有一个标签,显示文本“正确”或“不正确”,关于他们是否正确演唱歌曲,是否有办法使文本在正确演唱时变为绿色,在错误演唱时变为红色。我已经尝试了下面代码中列出的一些事情

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变量的默认值只是“”,它不是一种颜色,因此代码不起作用


Tags: mastergetcolumntkgridrowprintfont
2条回答

使用配置方法:

...
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变量

相关问题 更多 >