In this post you will learn to click the radio button and to change the background color according to the radio button you clicked.
When the radio buttons are clicked , the lambda function works. In the lambda function, the “color” function takes a parameter the is the color of background.
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 |
from tkinter import * root = Tk() root.title("Change background color with radio button") def changeColor(color): root.configure(background =color) choice1.configure(background =color) choice2.configure(background =color) choice3.configure(background =color) choice4.configure(background =color) v =StringVar() v.set("L") choice1 =Radiobutton(root, text ="red", value =1, variable =v, command =lambda: changeColor("red")) choice1.grid(row =0, column =0) choice2 =Radiobutton(root, text ="blue", value =2, variable =v, command =lambda: changeColor("blue")) choice2.grid(row =1, column =0) choice3 =Radiobutton(root, text ="yellow", value =3, variable =v, command =lambda: changeColor("yellow")) choice3.grid(row =2, column =0) choice4 =Radiobutton(root, text ="green", value =4, variable =v, command =lambda: changeColor("green")) choice4.grid(row =3, column =0) root.mainloop() |
Output:
Could you please explain the purpose of setting v to “L”??