Radio Button in Python

Radio button in 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.

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. Using this program we can execute radio button in python.

Tkinter Lambda Function in Python

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.

root.mainloop() in Python

mainloop() is a method on the main window that is called when we wish to run our program. This technique will run indefinitely, waiting for user input until the user departs the program — either by closing the window or by ending the program with a console keyboard interrupt.

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()