In this post we will learn how to use Ratio buttons on our Tkinter application. Lets get started,
IN[1]
import tkinter as tk
from tkinter import ttk
try :
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except :
pass
root = tk.Tk()
root.title("Radio Buttons")
root.geometry("600x400")
storage_variable = tk.StringVar()
option_1 = ttk.Radiobutton(root, text = "option 1", variable = storage_variable, value = "option 1").pack()
option_2 = ttk.Radiobutton(root, text = "option 2", variable = storage_variable, value = "option 2").pack()
option_3 = ttk.Radiobutton(root, text = "option 3", variable = storage_variable, value = "option 3").pack()
root.mainloop()
OUT[1]
EXPLANATION
Radio button is a part of ttk not tk package. Using Radiobutton function we can implement, but we have to interconnect all these radio buttons as when one is selected other have to be disabled. Content of these radio buttons text can be declared using text argument.
We can provide these awareness to the radio buttons by declaring a variable StringVar() and passing it as arguments inside the Radiobutton function.