Change background color with radio button in Tkinter python

Change background color with radio button in Tkinter python. Here the program we can change the background color using the radio button in the tkinter python program. A radio button, often known as an option button, is a graphical control element that allows the user to select only one option from a group of mutually incompatible choices.

from tkinter import * 
root = Tk()
root.title("Change the color with 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()

Why radio button is used called radio button

Each radio button is usually accompanied by a label that describes the option it represents. The user can choose one choice from a list of options using radio buttons.

If you believe the customer needs to view all possible alternatives side by side, you should utilise radio buttons for optional sets that are mutually incompatible.

When one of the radio buttons was pressed, the other buttons would pop out, leaving the pressed button as the sole one in the “pushed in” state.

Tkinter lambda function in python GUI program

Tkinter GUI programs benefit greatly from Lamda Functions (also known as Anonymous Functions in Python). They allow us to use the callback function to convey several pieces of data. Lambda can be used inside any function that acts as an anonymous expression function.

A lambda function in Python is a one-line function with no name that can take any number of arguments but only have one expression. Such a function can behave similarly to a standard function specified with the def keyword in Python.

Syntactically, lambda functions can only return a single expression. You can utilise them inside other functions as an anonymous function. There is no need for a return statement in lambda functions because they always return a single expression.