In this tutorial we will change the color of a label via radiobutton click.
First of all, we added 3 labels to the window . We created a changeColor function to change the color when we click on the labels. Then we got the colors of the radioButtons with StringVar() as “var”.
Python Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | from tkinter import * from tkinter import messagebox root = Tk() root.title("App") root.geometry("230x150") def changeColor(): #label.config(fg=var.get()) label["fg"] = var.get() var = StringVar() r1 = Radiobutton(root, text="Color: green", fg="green", variable=var, value="green", command=changeColor) r1.pack() r2 = Radiobutton(root, text="Color: red", fg="red", variable=var, value="red", command=changeColor) r2.pack() r3 = Radiobutton(root, text="Color: blue", fg="blue", variable=var, value="blue", command=changeColor) r3.pack() label = Label( text="Hello World" ) label.pack(pady=20) root.mainloop() |
Output: