How To Add A RadioButton In Tkinter In Python

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.   
  6.   
  7. radiobutton1=Radiobutton(a, text="Radiobutton 1",activebackground="black" ,  
  8.              activeforeground="green",bg="pink",bd=10,cursor = "target").pack()  
  9. radiobutton2=Radiobutton(a, text="Radiobutton 2",activebackground="black" ,  
  10.              activeforeground="green",bg="pink",bd=10,cursor = "target").pack()  
  11. radiobutton3=Radiobutton(a, text="Radiobutton 3",activebackground="black" ,  
  12.              activeforeground="green",bg="pink",bd=10,cursor = "target").pack()  
  13. radiobutton4=Radiobutton(a, text="Radiobutton 4",activebackground="black" ,  
  14.              activeforeground="green",bg="pink",bd=10,cursor = "target").pack()  
  15.   
  16.   
  17. 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.   
  6. radiobutton=Radiobutton(a, text="button 1",fg="blue",  
  7.             font="Castellar",height=3,width=10,relief="solid").pack()  
  8. radiobutton=Radiobutton(a, text="button 2",fg="red",  
  9.             font="Castellar",height=3,width=10,relief="solid").pack()  
  10. radiobutton=Radiobutton(a, text="button 3",fg="blue",  
  11.             font="Castellar",height=3,width=10,relief="solid").pack()  
  12. radiobutton=Radiobutton(a, text="button 4",fg="red",  
  13.             font="Castellar",height=3,width=10,relief="solid").pack()  
  14.   
  15. 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.   
  6. radiobutton=Radiobutton(a, text="button 1",state=DISABLED,  
  7.                  justify=RIGHT,padx=10,pady=10).pack()  
  8. radiobutton=Radiobutton(a, text="button 2",state=DISABLED,  
  9.                 justify=RIGHT,padx=10,pady=10).pack()  
  10. radiobutton=Radiobutton(a, text="button 3",state=DISABLED,  
  11.                 justify=RIGHT,padx=10,pady=10).pack()  
  12. radiobutton=Radiobutton(a, text="button 4",state=DISABLED,  
  13.                 justify=RIGHT,padx=10,pady=10).pack()  
  14.   
  15. 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!


Recommended Free Ebook
Similar Articles