- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
- Python 3 Useful Resources
- Python 3 - Questions and Answers
- Python 3 - Quick Guide
- Python 3 - Tools/Utilities
- Python 3 - Useful Resources
- Python 3 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python 3 - Tkinter Radiobutton
This widget implements a multiple-choice button, which is a way to offer many possible selections to the user and lets user choose only one of them.
In order to implement this functionality, each group of radiobuttons must be associated to the same variable and each one of the buttons must symbolize a single value. You can use the Tab key to switch from one radionbutton to another.
Here is the simple syntax to create this widget −
w = Radiobutton ( master, option, ... )
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
|Sr.No.||Option & Description|
The background color when the mouse is over the radiobutton.
The foreground color when the mouse is over the radiobutton.
If the widget inhabits a space larger than it needs, this option specifies where the radiobutton will sit in that space. The default is anchor = CENTER.
The normal background color behind the indicator and label.
To display a monochrome image on a radiobutton, set this option to a bitmap.
The size of the border around the indicator part itself. Default is 2 pixels.
A procedure to be called every time the user changes the state of this radiobutton.
If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the radiobutton.
The font used for the text.
The color used to render the text.
The number of lines (not pixels) of text on the radiobutton. Default is 1.
The color of the focus highlight when the radiobutton does not have focus.
The color of the focus highlight when the radiobutton has the focus.
To display a graphic image instead of text for this radiobutton, set this option to an image object.
If the text contains multiple lines, this option controls how the text is justified: CENTER (the default), LEFT, or RIGHT.
How much space to leave to the left and right of the radiobutton and text. Default is 1.
How much space to leave above and below the radiobutton and text. Default is 1.
Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.
The color of the radiobutton when it is set. Default is red.
If you are using the image option to display a graphic instead of text when the radiobutton is cleared, you can set the selectimage option to a different image that will be displayed when the radiobutton is set.
The default is state = NORMAL, but you can set state = DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the radiobutton, the state is ACTIVE.
The label displayed next to the radiobutton. Use newlines ("\n") to display multiple lines of text.
To slave the text displayed in a label widget to a control variable of class StringVar, set this option to that variable.
You can display an underline (_) below the nth letter of the text, counting from 0, by setting this option to n. The default is underline = -1, which means no underlining.
When a radiobutton is turned on by the user, its control variable is set to its current value option. If the control variable is an IntVar, give each radiobutton in the group a different integer value option. If the control variable is a StringVar, give each radiobutton a different string value option.
The control variable that this radiobutton shares with the other radiobuttons in the group. This can be either an IntVar or a StringVar.
Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
You can limit the number of characters in each line by setting this option to the desired number. The default value, 0, means that lines will be broken only at newlines.
|Sr.No.||Method & Description|
Clears (turns off) the radiobutton.
Flashes the radiobutton a few times between its active and normal colors, but leaves it the way it started.
You can call this method to get the same actions that would occur if the user clicked on the radiobutton to change its state.
Sets (turns on) the radiobutton.
Try the following example yourself −
# !/usr/bin/python3 from tkinter import * def sel(): selection = "You selected the option " + str(var.get()) label.config(text = selection) root = Tk() var = IntVar() R1 = Radiobutton(root, text = "Option 1", variable = var, value = 1, command = sel) R1.pack( anchor = W ) R2 = Radiobutton(root, text = "Option 2", variable = var, value = 2, command = sel) R2.pack( anchor = W ) R3 = Radiobutton(root, text = "Option 3", variable = var, value = 3, command = sel) R3.pack( anchor = W) label = Label(root) label.pack() root.mainloop()
When the above code is executed, it produces the following result −