วิดเจ็ต Radiobutton ใน Tkinter อนุญาตให้ผู้ใช้เลือกเพียงหนึ่งตัวเลือกจากชุดตัวเลือกที่กำหนด Radiobutton มีค่าเพียงสองค่า คือ True หรือ False
หากเราต้องการรับค่าออกมาตรวจสอบว่าผู้ใช้เลือกตัวเลือกไหน เราสามารถใช้เมธอด get() ได้ มันจะคืนค่าอ็อบเจ็กต์ที่ถูกกำหนดเป็นตัวแปร เราสามารถแสดงการเลือกในวิดเจ็ต Label โดยการแปลงค่า integer เป็น string object และส่งผ่านในแอตทริบิวต์ text
ตัวอย่าง
# 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()
ผลลัพธ์
การรันโค้ดด้านบนจะแสดงหน้าต่างพร้อมชุด radiobutton คลิกตัวเลือกใดตัวเลือกหนึ่งและจะแสดงตัวเลือกที่คุณเลือก
