【问题标题】:Change the color of a label based on user input根据用户输入更改标签的颜色
【发布时间】:2021-03-21 18:04:58
【问题描述】:

我使用 Tkinter 创建了一个歌曲猜测器程序,我拥有它,因此用户按下按钮播放歌曲,他们输入他们认为的歌曲,然后在听完并猜测所有歌曲后,他们按下提交。现在在输入框的右侧,我有一个标签,显示文本“正确”或“不正确”,说明他们是否正确地听了这首歌,有没有办法让这个文本在他们得到它时变成绿色当他们弄错时,正确和红色。我已经尝试了下面代码中列出的一些东西。

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 变量的默认值只是 "" 这不是颜色,因此代码不起作用

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您需要使用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 变量。

    【讨论】:

      【解决方案2】:

      使用配置方法:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-06
        • 1970-01-01
        • 1970-01-01
        • 2018-07-08
        • 2020-09-20
        • 1970-01-01
        • 2018-12-01
        • 2021-02-10
        相关资源
        最近更新 更多