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
  1. 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
  1. from tkinter import *
  2. a=Tk()
  3. a.geometry("400x400")
  4. a.title("test")
  5. radiobutton1=Radiobutton(a, text="Radiobutton 1",activebackground="black" ,
  6. activeforeground="green",bg="pink",bd=10,cursor = "target").pack()
  7. radiobutton2=Radiobutton(a, text="Radiobutton 2",activebackground="black" ,
  8. activeforeground="green",bg="pink",bd=10,cursor = "target").pack()
  9. radiobutton3=Radiobutton(a, text="Radiobutton 3",activebackground="black" ,
  10. activeforeground="green",bg="pink",bd=10,cursor = "target").pack()
  11. radiobutton4=Radiobutton(a, text="Radiobutton 4",activebackground="black" ,
  12. activeforeground="green",bg="pink",bd=10,cursor = "target").pack()
  13. a.mainloop()
Output
How To Add A RadioButton In Tkinter In Python
  • 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
  1. from tkinter import *
  2. a=Tk()
  3. a.geometry("400x400")
  4. a.title("test")
  5. radiobutton=Radiobutton(a, text="button 1",fg="blue",
  6. font="Castellar",height=3,width=10,relief="solid").pack()
  7. radiobutton=Radiobutton(a, text="button 2",fg="red",
  8. font="Castellar",height=3,width=10,relief="solid").pack()
  9. radiobutton=Radiobutton(a, text="button 3",fg="blue",
  10. font="Castellar",height=3,width=10,relief="solid").pack()
  11. radiobutton=Radiobutton(a, text="button 4",fg="red",
  12. font="Castellar",height=3,width=10,relief="solid").pack()
  13. a.mainloop()
Output
How To Add A RadioButton In Tkinter In Python
  • 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
  1. from tkinter import *
  2. a=Tk()
  3. a.geometry("400x400")
  4. a.title("test")
  5. radiobutton=Radiobutton(a, text="button 1",state=DISABLED,
  6. justify=RIGHT,padx=10,pady=10).pack()
  7. radiobutton=Radiobutton(a, text="button 2",state=DISABLED,
  8. justify=RIGHT,padx=10,pady=10).pack()
  9. radiobutton=Radiobutton(a, text="button 3",state=DISABLED,
  10. justify=RIGHT,padx=10,pady=10).pack()
  11. radiobutton=Radiobutton(a, text="button 4",state=DISABLED,
  12. justify=RIGHT,padx=10,pady=10).pack()
  13. a.mainloop()
Output
How To Add A RadioButton In Tkinter In Python

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!