python - 用于更改文本颜色的 RadioButtons 命令 (tkinter)

标签 python tkinter

我在这个项目中使用 tkinter 输入一些单选按钮后,我成功地在单击时更改了窗口本身的背景颜色。但是,在添加另一个文本小部件后,我想在单击这些单选按钮时更改该文本的颜色。希望在这个简单的命令中得到您的帮助:

from tkinter import *

root = Tk()
root.title("WOOSAL")

def changeColourF(colour):
    root.configure(background =colour)
    choice1.configure(background =colour)
    choice2.configure(background =colour)
    choice3.configure(background =colour)
    choice4.configure(background =colour)

v =StringVar()
v.set("L")

choice1 =Radiobutton(root, text ="red", value =1, variable =v, command =lambda: changeColourF("red"))
choice1.grid(row =0, column =0)

choice2 =Radiobutton(root, text ="blue", value =2, variable =v, command =lambda: changeColourF("blue"))
choice2.grid(row =0, column =1)

choice3 =Radiobutton(root, text ="yellow", value =3, variable =v, command =lambda: changeColourF("yellow"))
choice3.grid(row =0, column =2)

choice4 =Radiobutton(root, text ="green", value =4, variable =v, command =lambda: changeColourF("green"))
choice4.grid(row =0, column =3)

w = Label(root, text="Hello Tkinter!")
w.grid(row=1, column=0)

root.mainloop()

最佳答案

您可以使用fg参数更改文本颜色。

def changeColourF(colour):
    root.configure(background=colour)
    choice1.configure(background=colour)
    choice2.configure(background=colour)
    choice3.configure(background=colour)
    choice4.configure(background=colour)
    w.configure(fg=colour)

enter image description here

关于python - 用于更改文本颜色的 RadioButtons 命令 (tkinter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59093377/

相关文章:

python - 如何插入分钟 :seconds format time interval to postgresql in right way?

python - SQLAlchemy/Flask - 获取关系表中的行数

python - python 图像错误

python - 由于焦点位于前一帧的条目,Tkinter 绑定(bind)方法不起作用

python - 安装 python3-tk 包时出错

python - 如何从gekko中获取 'infeasibilities.txt'

python - 从 Python 中的集合中有效地找到最近的坐标对

Python Tkinter While 线程

Python:Tkinter:为什么是 root.mainloop() 而不是 app.mainloop()

python - 为什么 deepcopy 不创建对 lambda 函数的新引用?