gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:03:35 26 4
gpt4 key购买 nike

我在这个项目中使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com