Introduction
In this article, I will explain how to add a Radiobutton in Tkinter in Python.
Definition
The Radiobutton keyword is used as a multiple-choice button, which is a way to offer many possible selections to the user and let the user choose only one of them.
Syntax
- w = Radiobutton ( a, option)
Parameters
a − This represents the parent window of the computer.
option − The list of most used options for this widget.
Option & Description
-
active background - The active background is used to change Background color when the mouse is over the radio button.
-
active foreground - The active foreground is used to change the Foreground color when the mouse is over the radio button.
-
bd - The bd is used to change the border width in pixels. The default is 2.
-
bg - The bg is used to change the Normal background color background.
-
Cursor - The cursor is used to set this option to a cursor name, the mouse cursor will change to that pattern when it is over the radio button
Program
- from tkinter import *
- a=Tk()
- a.geometry("400x400")
- a.title("test")
-
-
- radiobutton1=Radiobutton(a, text="Radiobutton 1",activebackground="black" ,
- activeforeground="green",bg="pink",bd=10,cursor = "target").pack()
- radiobutton2=Radiobutton(a, text="Radiobutton 2",activebackground="black" ,
- activeforeground="green",bg="pink",bd=10,cursor = "target").pack()
- radiobutton3=Radiobutton(a, text="Radiobutton 3",activebackground="black" ,
- activeforeground="green",bg="pink",bd=10,cursor = "target").pack()
- radiobutton4=Radiobutton(a, text="Radiobutton 4",activebackground="black" ,
- activeforeground="green",bg="pink",bd=10,cursor = "target").pack()
-
-
- a.mainloop()
Output
-
fg - The fg is used to change the normal foreground (text) color.
-
font - The font is used to change the text font .
-
height - The height is used to change the height on the radio button. in text lines (text buttons) or pixels (images).
-
width - The width is used to change the width on the radio button in letters.
-
Relief - Relief is used to specify the type of the border. The values are SUNKEN, RAISED, GROOVE, and RIDGE.
Program
- from tkinter import *
- a=Tk()
- a.geometry("400x400")
- a.title("test")
-
- radiobutton=Radiobutton(a, text="button 1",fg="blue",
- font="Castellar",height=3,width=10,relief="solid").pack()
- radiobutton=Radiobutton(a, text="button 2",fg="red",
- font="Castellar",height=3,width=10,relief="solid").pack()
- radiobutton=Radiobutton(a, text="button 3",fg="blue",
- font="Castellar",height=3,width=10,relief="solid").pack()
- radiobutton=Radiobutton(a, text="button 4",fg="red",
- font="Castellar",height=3,width=10,relief="solid").pack()
-
- a.mainloop()
Output
-
State - The state is used to set this option to DISABLED to gray out the radio button and make it unresponsive.
-
Justify - The justify is used to change the arrangement order LEFT, CENTER, RIGHT.
-
Padx - The padx is used to change the additional padding left and right of the text.
-
Pady - The pady is used to changing the additional padding above and below the text.
Program
- from tkinter import *
- a=Tk()
- a.geometry("400x400")
- a.title("test")
-
- radiobutton=Radiobutton(a, text="button 1",state=DISABLED,
- justify=RIGHT,padx=10,pady=10).pack()
- radiobutton=Radiobutton(a, text="button 2",state=DISABLED,
- justify=RIGHT,padx=10,pady=10).pack()
- radiobutton=Radiobutton(a, text="button 3",state=DISABLED,
- justify=RIGHT,padx=10,pady=10).pack()
- radiobutton=Radiobutton(a, text="button 4",state=DISABLED,
- justify=RIGHT,padx=10,pady=10).pack()
-
- a.mainloop()
Output
Conclusion
In this article, we have seen how to add a radiobutton in Tkinter in Python. I hope this article was useful to you. Thanks for reading!