Best post on Button, Checkbutton & Radiobutton Widget in Python

Button, Checkbutton & Radiobutton Widget in Python

 

Button

The Button widget is a standard Tkinter widget used to implement various kinds of buttons.
Buttons can contain text or images, and you can associate a Python function or method with each button.
While labels can display text in various fonts, a button can only display text in a single font. The text of a button can span more than one line.
When the button is pressed, Tkinter automatically calls that function or method.
In addition, one of the characters can be underlined, for example to mark a keyboard shortcut.
By default, the Tab key can be used to move to a button widget.

Syntax
Here is the simple syntax to create this widget −

w = Button( master, option=value, ... )

Parameters
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.
Various options of Button widget:

  • activebackground : Background color when the button is under the cursor.
  • activeforeground : Foreground color when the button is under the cursor.
  • anchor: This option controls where the text is positioned. The default anchor is CENTER.Other possible values are NW(North West is top left), N, NE(North East is top right), W, E, SW(South West is bottom left), S, SE(South East is bottom right)
  • bg : The normal background color displayed behind the label and indicator.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • command : Function or method to be called when the button is clicked.
  • bitmap : Set this option equal to a bitmap or image object and the label will display that graphic.
  • font : If you are displaying text in this label (with the text or textvariable option, the font option specifies in what font that text will be displayed.
  • fg : If you are displaying text or a bitmap in this label, this option specifies the color of the text. If you are displaying a bitmap, this is the color that will appear at the position of the 1-bits in the bitmap.
  • height : The vertical dimension of the new frame.
  • image : To display a static image in the label widget, set this option to an image object.
  • text : To display one or more lines of text in a label widget, set this option to a string containing the text. Internal newlines (“n”) will force a line break.
  • underline : 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.
  • width: Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :19th March 2020

Description : Program to show use of Button widget

"""
from tkinter import *
def display():
    #config method is used to configure various widgets
    label.config(text="Welcome to itvoyagers.in",bg="black",fg="red")
    label.config(font=("times",50))
root=Tk()
Button(root,text="Click me",command=display).pack()
label=Label(root,text="")
label.pack()

OUTPUT

Output of Button widget

Checkbutton

The Checkbutton widget is a standard Tkinter widgets used to implement on-off selections.It is used to toggle between many options and can be used to select many options at same time.
Checkbuttons can contain text or images, and can be associated with a Python function or method.
When the button is pressed, Tkinter calls that function or method.
Each Checkbutton widget should be associated with a variable.
By default, the Tab key can be used to move to a button widget.

Syntax
Here is the simple syntax to create this widget −

w = Checkbutton( master, option=value, ... )

Parameters
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.
Various options of Checkbutton widget:

  • activebackground : Background color when the button is under the cursor.
  • activeforeground : Foreground color when the button is under the cursor.
  • bg : The normal background color displayed behind the label and indicator.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • command : Function or method to be called when the button is clicked.
  • cursor : The mouse pointer will be changed to the cursor name when it is over the checkbutton.
  • bitmap : Set this option equal to a bitmap or image object and the label will display that graphic.
  • font : It represents the font of the checkbutton.
  • fg : The foreground color (text color) of the checkbutton.
  • height : The vertical dimension of the new frame.
  • image : To display a static image in the label widget, set this option to an image object.
  • variable : The control variable that tracks the current state of the checkbutton. Normally this variable is an IntVar, and 0 means cleared and 1 means set, but see the offvalue and onvalue options above.
  • state : The default is state=NORMAL, but you can use state=DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE.
  • text : To display one or more lines of text in a label widget, set this option to a string containing the text. Internal newlines (“n”) will force a line break.
  • offvalue : The associated control variable is set to 0 by default if the button is unchecked. We can change the state of an unchecked variable to some other one.
  • onvalue : The associated control variable is set to 1 by default if the button is checked. We can change the state of the checked variable to some other one.
  • width: Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :19th March 2020

Description : Program to show use of Checkbutton widget

"""
from tkinter import *
master=Tk()
itv1=StringVar()
itv1.set("FAVOURITE SUBJECTS")
Label(master, textvariable=itv1,bg="black",fg="white").grid(row =0,sticky=W)
var1=IntVar()
Checkbutton(master ,text="Python Programming",variable=var1,fg='green').grid(row=1,sticky=W)
var2=IntVar()
Checkbutton(master ,text="Linux",variable=var2,fg='blue').grid(row=2,sticky=W)
var3=IntVar()
Checkbutton(master ,text="Business Intelligence",variable=var3,fg='red').grid(row=3,sticky=W)
var4=IntVar()
Checkbutton(master ,text="Artificial Intelligence",variable=var4,fg='orange').grid(row=4,sticky=W)
var5=IntVar()
Checkbutton(master ,text="Big Data Analytics",variable=var5,fg='violet').grid(row=5,sticky=W)

OUTPUT

Output of Checkbutton widget

Radiobutton

This widget allows user to select only one out of multiple selections unlike checkbutton widget which allows many of many selections.
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.

Radio buttons can contain text or images.
The button can only display text in a single font.
A Python function or method can be associated with a radio button. This function or method will be called, if you press this radio button.

By default, the Tab key can be used to move to a button widget.

Syntax
Here is the simple syntax to create this widget −

w = Radiobutton( master, option=value, ... )

Parameters
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.
Various options of Radiobutton  widget:

  • anchor : It represents the exact position of the text within the widget if the widget contains more space than the requirement of the text. The default value is CENTER.
  • activebackground : Background color when the button is under the cursor.
  • activeforeground : Foreground color when the button is under the cursor.
  • bg : The normal background color displayed behind the label and indicator.
  • command : Function or method to be called when the button is clicked.
  • cursor : The mouse pointer will be changed to the cursor name when it is over the checkbutton.
  • bitmap : Set this option equal to a bitmap or image object and the label will display that graphic.
  • font : It represents the font of the checkbutton.
  • fg : The foreground color (text color) of the checkbutton.
  • height : The vertical dimension of the new frame.
  • image : To display a static image in the label widget, set this option to an image object.
  • variable : The control variable that tracks the current state of the checkbutton. Normally this variable is an IntVar, and 0 means cleared and 1 means set, but see the offvalue and onvalue options above.
  • state : The default is state=NORMAL, but you can use state=DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE.
  • text : To display one or more lines of text in a label widget, set this option to a string containing the text. Internal newlines (“n”) will force a line break.
  • width: Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.
  • selectcolor : The color of the radio button when it is selected.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :19th March 2020

Description : Program to show use of Radiobutton widget

"""
from tkinter import *
master=Tk()
def selection():  
   selection = "You selected the option " + str(var1.get())  
   l1.config(text = selection)  
itv1=StringVar()
itv1.set("FAVOURITE SUBJECTS")
Label(master, textvariable=itv1,bg="black",fg="white").grid(row =0,sticky=W)
var1=IntVar() #only 1 variable is required 
Radiobutton(master ,text="Python Programming",variable=var1,value = 1,fg='green',command=selection).grid(row=1,sticky=W)

Radiobutton(master ,text="Linux",variable=var1,value = 2,fg='blue',command=selection).grid(row=2,sticky=W)

Radiobutton(master ,text="Business Intelligence",variable=var1,value = 3,fg='red',command=selection).grid(row=3,sticky=W)

Radiobutton(master ,text="Artificial Intelligence",variable=var1,value = 4,fg='orange',command=selection).grid(row=4,sticky=W)

Radiobutton(master ,text="Big Data Analytics",variable=var1,value = 5,fg='violet',command=selection).grid(row=5,sticky=W)
l1=Label(master)
l1.grid(row=6,column=2)


OUTPUT

Output of Radiobutton widgets

Difference between Checkbutton and Radiobutton widget

Checkbutton WidgetRadiobutton Widget
Allows many of many selectionsAllows one of many selections
Same number of variables need to be defined as number of optionsOnly one variable is defined irrespective of number of options
A checkbutton is an input option that represents a setting or value with an on, off, or mixed-choice. A check mark within the checkbutton indicates that the setting is selected or checked.A radio button is typically represented as a round control to distinguish from the square checkbutton control.
eg: Selecting hobbieseg: Selecting gender
from tkinter import *
window =Tk()
l= Label(window,text="Hobbies").pack()
c1=Checkbutton(window,text="dancing").pack()
c2=Checkbutton(window,text="cricket").pack()
c3=Checkbutton(window,text="singing").pack()
c4=Checkbutton(window,text="reading").pack()
c5=Checkbutton(window,text="travelling").pack()
c6=Checkbutton(window,text="writing").pack()
c7=Checkbutton(window,text="football").pack()
c8=Checkbutton(window,text="cooking").pack()
from tkinter import *
master=Tk()
var1=IntVar()
Radiobutton(master ,text="Male",variable=var1,value=1).grid(row=0,sticky=W)

#var2=IntVar()
Radiobutton(master ,text="Female",variable=var1,value=2).grid(row=1,sticky=W)

You can also check following posts to know more about tkinter and GUI

Leave a Comment