- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to select only one Radiobutton in Tkinter?
To implement the selection for one or more options, we can use the Radiobutton widget. The Radiobutton widget in Tkinter allows the user to make a selection for only one option from a set of given choices. The Radiobutton has only two Boolean values: True or False.
If we want to get the output to check which option the user has selected, then we can use the get() method. It returns the object that is defined as the variable. We can display the selection in a label widget by casting the integer value in a string object and pass it in the text attributes.
# 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 have selected " + 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()
Running the above code will display a window with a label widget and a set of radiobuttons that correspond to an option. Select any option from the list to see the output.
- Related Articles
- How to get radiobutton output in Tkinter?
- How to select distinct value from one MySQL column only?
- How to add padding to a tkinter widget only on one side?
- How to set action to a RadioButton in JavaFX?
- How to select only numeric strings in MongoDB?
- How to close only the TopLevel window in Python Tkinter?
- How to make the Tkinter text widget read only?
- How to correctly select multiple items with the mouse in Tkinter Treeview?
- How to select only MySQL date from datetime column?
- How to style a select dropdown with only CSS?
- How to update only one property in MongoDB?
- MySQL query to skip the duplicate and select only one from the duplicated values
- How to select at the same time from two Tkinter Listbox?
- How to select a directory and store the location using Tkinter in Python?
- How to bind multiple events with one "bind" in Tkinter?