Friday, December 18, 2015

Python Tkinter: Radiobutton



Example to implement Radiobutton of Python Tkinter.
#for Python 2
#import Tkinter as tk
#for Python 3
import tkinter as tk

import platform

def quit():
    global tkTop
    tkTop.destroy()

tkTop = tk.Tk()
tkTop.geometry('500x300')

tkLabelTop = tk.Label(tkTop, text=" http://hello-python.blogspot.com ")
tkLabelTop.pack()

strVersion = "running Python version " + platform.python_version()
tkLabelVersion = tk.Label(tkTop, text=strVersion)
tkLabelVersion.pack()
strPlatform = "Platform: " + platform.platform()
tkLabelPlatform = tk.Label(tkTop, text=strPlatform)
tkLabelPlatform.pack()

tkButtonQuit = tk.Button(
    tkTop,
    text="Quit",
    command=quit)
tkButtonQuit.pack()

def rbCallback():
    varLabel.set("Radiobutton clicked: " + str(rbVar.get()))

rbVar = tk.IntVar()

rb1 = tk.Radiobutton(
    tkTop,
    text="One",
    variable=rbVar,
    value=1,
    command=rbCallback)
rb1.pack(anchor=tk.W)

rb2 = tk.Radiobutton(
    tkTop,
    text="Two",
    variable=rbVar,
    value=2,
    command=rbCallback)
rb2.pack(anchor=tk.W)

rb3 = tk.Radiobutton(
    tkTop,
    text="Three",
    variable=rbVar,
    value=3,
    command=rbCallback)
rb3.pack(anchor=tk.W)

varLabel = tk.StringVar()
tkLabel = tk.Label(tkTop, textvariable=varLabel)
tkLabel.pack()

tk.mainloop()