How to select only single option from many in Tkinter GUI application : Radiobutton Widget


Radiobutton Widget

Allows user to select one out of many options


Syntax:

            Radiobutton(master, options.........)

master : master is nothing but the window object (parent window for which Radiobutton needs to be created )

options : options are used to shape the label separated by comma's and to define action on click. Below is the list of options, can be passed while creating Radiobutton.

  • activebackground
  • activeforeground
  • anchor
  • bg
  • bitmap
  • borderwidth
  • command
  • cursor
  • font
  • fg
  • height
  • highlightbackground
  • highlightcolor
  • image
  • justify
  • padx
  • pady
  • relief
  • selectcolor
  • selectimage
  • state
  • text
  • textvariable
  • underline
  • value
  • variable
  • width
  • wraplength


Radiobutton built in Methods
  • deselect()
    • Clears the Radiobutton.
  • flash()
    • Flashes the Radiobutton a few times between its active and normal colors, but leaves it the way it started.
  • invoke()
    • To get the same actions that would occur if the user clicked on the Radiobutton to change its state.
  • select()
    • Sets the Radiobutton.


there are few options which may not work on mac, but work finely on windows and linux

Example for the Radiobutton widget, output screen shot is on top of this post


# program to create radiobutton widget

try:
from Tkinter import *
except:
from tkinter import *


mw = Tk()
mw.geometry(
'400x100+10+10')
mw.title(
"AETies : Automation Expert Technologies")
var = IntVar()

label = Label(mw
, text='Select your favourite subject')
label.pack()

ch_btn1 = Radiobutton(mw
, text="Python", variable=var, value=1)
ch_btn1.pack()

ch_btn2 = Radiobutton(mw
, text="Embedded", variable=var, value=2)
ch_btn2.pack()

ch_btn3 = Radiobutton(mw
, text="Language C", variable=var, value=3)
ch_btn3.pack()

mw.mainloop()




Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India