Python 3 – Tkinter Radiobutton
Radiobutton is a graphical user interface component in Tkinter module that allows users to choose one option from a set of multiple options by selecting a single radio button. It is a circular-shaped button that displays a text label, which is usually used to present multiple options to the user and allows the user to choose a single option.
Radiobutton in Tkinter is intuitive and straightforward to use. It automatically updates the selected option when the user clicks on any of the circular buttons. This feature saves developers a lot of time in tracking the user’s choice, and it is useful when you have multiple options for the user to choose from.
Creating Radiobutton in Tkinter
We can create a Radiobutton in Tkinter by importing the Radiobutton from the Tkinter module. Below code demonstrates how to create Radiobutton in Tkinter:
from tkinter import *
root = Tk()
root.geometry("400x400")
var = IntVar()
var.set(1)
Radiobutton(root, text="Option 1", variable=var, value=1).pack()
Radiobutton(root, text="Option 2", variable=var, value=2).pack()
Radiobutton(root, text="Option 3", variable=var, value=3).pack()
root.mainloop()
In this code, we created a Tkinter window and assigned it to the variable ‘root’. We then configured the window size with the geometry() method. After that, we created an Integer variable ‘var’ and set its value to 1 by using the ‘set()’ method.
Next, we created three Radiobuttons with different text labels and assigned them to the ‘root’ window. We also specified the ‘variable’ to which the Radiobutton belongs to and set the ‘value’ of each Radiobutton to a unique integer value.
Finally, we set the ‘mainloop()’ method to keep the Window running until the user closes the window.
Accessing the selected Radiobutton value
We can access the value of the selected Radiobutton by accessing the variable associated with the button. The variable’s value will be automatically updated when the user clicks on a Radiobutton. Below code demonstrates how to access the selected Radiobutton’s value:
from tkinter import *
root = Tk()
root.geometry("400x400")
var = IntVar()
var.set(1)
Radiobutton(root, text="Option 1", variable=var, value=1).pack()
Radiobutton(root, text="Option 2", variable=var, value=2).pack()
Radiobutton(root, text="Option 3", variable=var, value=3).pack()
def show_selected():
print("Selected Option: ", var.get())
Button(root, text="Submit", command=show_selected).pack()
root.mainloop()
In this code, we created a Tkinter window and assigned it to the variable ‘root’. We then created an Integer variable ‘var’ and set its value to 1. After that, we created three Radiobuttons with different text labels and assigned them to the ‘root’ window. We also specified the ‘variable’ to which the Radiobutton belongs to and set the ‘value’ of each Radiobutton to a unique integer value.
We then created a function ‘show_selected()’ that will print the selected Radiobutton’s value using the ‘get()’ method associated with the ‘var’ variable.
Finally, we created a ‘Submit’ button that, when clicked, will execute the ‘show_selected()’ function and print the selected Radiobutton’s value in the console.
Conclusion
In conclusion, Radiobutton in Tkinter is an essential tool for creating user-friendly graphical user interfaces. It is a simple and intuitive tool that saves developers a lot of time in tracking the user’s choices. Radiobuttons are useful when you need the user to select a single option from multiple options in a Tkinter application. In this article, we covered how to create Radiobuttons in Tkinter and how to access the selected value. By understanding how to use Radiobuttons in Tkinter, you can create more robust and user-friendly interfaces for your Tkinter application.