Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Tkinter - Checkbutton Widget
Next article icon

TheRadiobutton is a standardTkinter widget used to implement one-of-many selections.Radiobuttons can contain text or images, and you can associate a Python function or method with each button. When the button is pressed,Tkinter automatically calls that function or method.
Syntax: 
 

button = Radiobutton(master, text="Name on Button", variable = "shared variable", value = "values of each button", options = values, ...)
shared variable = A Tkinter variable shared among all Radio buttons 
value = each radiobutton should have different value otherwise more than 1 radiobutton will get selected. 
 


Code #1: 
Radio buttons, but not in the form of buttons, in form ofbutton box. In order to display button box,indicatoron/indicator option should be set to 0. 
 

Python3
# Importing Tkinter modulefromtkinterimport*# from tkinter.ttk import *# Creating master Tkinter windowmaster=Tk()master.geometry("175x175")# Tkinter string variable# able to store any string valuev=StringVar(master,"1")# Dictionary to create multiple buttonsvalues={"RadioButton 1":"1","RadioButton 2":"2","RadioButton 3":"3","RadioButton 4":"4","RadioButton 5":"5"}# Loop is used to create multiple Radiobuttons# rather than creating each button separatelyfor(text,value)invalues.items():Radiobutton(master,text=text,variable=v,value=value,indicator=0,background="light blue").pack(fill=X,ipady=5)# Infinite loop can be terminated by# keyboard or mouse interrupt# or by any predefined function (destroy())mainloop()

Output: 
 


The background of these button boxes is light blue. Button boxes having a white backgrounds as well as sunken are selected ones.
 


  
Code #2: Changing button boxes into standard radio buttons. For this remove indicatoron option. 
 

Python3
# Importing Tkinter modulefromtkinterimport*fromtkinter.ttkimport*# Creating master Tkinter windowmaster=Tk()master.geometry("175x175")# Tkinter string variable# able to store any string valuev=StringVar(master,"1")# Dictionary to create multiple buttonsvalues={"RadioButton 1":"1","RadioButton 2":"2","RadioButton 3":"3","RadioButton 4":"4","RadioButton 5":"5"}# Loop is used to create multiple Radiobuttons# rather than creating each button separatelyfor(text,value)invalues.items():Radiobutton(master,text=text,variable=v,value=value).pack(side=TOP,ipady=5)# Infinite loop can be terminated by# keyboard or mouse interrupt# or by any predefined function (destroy())mainloop()

Output: 


 


These Radiobuttons are created usingtkinter.ttk that is why background option is not available but we can usestyle class to do styling. 
 
Code #3: Adding Style to Radio Button using style class. 
 

Python3
# Importing Tkinter modulefromtkinterimport*fromtkinter.ttkimport*# Creating master Tkinter windowmaster=Tk()master.geometry('175x175')# Tkinter string variable# able to store any string valuev=StringVar(master,"1")# Style class to add style to Radiobutton# it can be used to style any ttk widgetstyle=Style(master)style.configure("TRadiobutton",background="light green",foreground="red",font=("arial",10,"bold"))# Dictionary to create multiple buttonsvalues={"RadioButton 1":"1","RadioButton 2":"2","RadioButton 3":"3","RadioButton 4":"4","RadioButton 5":"5"}# Loop is used to create multiple Radiobuttons# rather than creating each button separatelyfor(text,value)invalues.items():Radiobutton(master,text=text,variable=v,value=value).pack(side=TOP,ipady=5)# Infinite loop can be terminated by# keyboard or mouse interrupt# or by any predefined function (destroy())mainloop()

Output: 


 


You may observe that font style is changed as well as background and foreground colors are also changed. Here, TRadiobutton is used in style class, it automatically applies styling to all the available Radiobuttons.
 


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp