0

我是 tkinter 的新手,当我按下“Return”时,我正在尝试更改标签中特定单词的颜色和背景。该代码有效,但它改变了整个标签的颜色,我不知道如何指定它,所以它只改变标签上第一个单词的颜色(w_1)。这是我的代码:

,谢谢!

from tkinter import *
import random

root = Tk()

list_1 = ["about","above","add","after","again","air","all","almost","along","also","always","America","an"]
w_1 = random.choice(list_1)
w_2 = random.choice(list_1)
w_3 = random.choice(list_1)
w_4 = random.choice(list_1)
w_5 = random.choice(list_1)

def color(event):
   list_label.config(fg="#808080")

list_label = Label(root, text=w_1 + " " + w_2 + " " + w_3 + " " + w_4 + " " + w_5, width=30)
list_label.grid(row=1, column=0, pady=5, padx=150)

root.bind("<Return>", color)

root.mainloop()


4

0 回答 0