टिंकर में रेडियोबटन विजेट उपयोगकर्ता को दिए गए विकल्पों के सेट से केवल एक विकल्प के लिए चयन करने की अनुमति देता है। रेडियोबटन में केवल दो मान होते हैं, या तो सही या गलत।
यदि हम यह जाँचने के लिए आउटपुट प्राप्त करना चाहते हैं कि उपयोगकर्ता ने कौन सा विकल्प चुना है, तो हम उपयोग कर सकते हैं get() तरीका। यह उस वस्तु को लौटाता है जिसे चर के रूप में परिभाषित किया गया है। हम एक स्ट्रिंग ऑब्जेक्ट में पूर्णांक मान कास्ट करके एक लेबल विजेट में चयन प्रदर्शित कर सकते हैं और इसे टेक्स्ट विशेषताओं में पास कर सकते हैं।
उदाहरण
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
# Define a function to get the output for selected option
def selection():
selected = "You selected the option " + str(radio.get())
label.config(text=selected)
radio = IntVar()
Label(text="Your Favourite programming language:", font=('Aerial 11')).pack()
# Define radiobutton for each options
r1 = Radiobutton(win, text="C++", variable=radio, value=1, command=selection)
r1.pack(anchor=N)
r2 = Radiobutton(win, text="Python", variable=radio, value=2, command=selection)
r2.pack(anchor=N)
r3 = Radiobutton(win, text="Java", variable=radio, value=3, command=selection)
r3.pack(anchor=N)
# Define a label widget
label = Label(win)
label.pack()
win.mainloop() आउटपुट
उपरोक्त कोड को निष्पादित करने से एक विंडो प्रदर्शित होगी जिसमें रेडियो बटन का एक सेट होगा। किसी भी विकल्प पर क्लिक करें और यह आपके द्वारा चुने गए विकल्प को दिखाएगा।
