Tuesday, March 26, 2013

CheckBox of tkinter

Example of using tkinter.Checkbutton in Python. Also demonstrate how to get status of tkinter.Checkbutton.

tkinter.CheckBox
tkinter.CheckBox


#tkinter for Python 3.x
#Tkinter for Python 2.x

import tkinter

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

def setCheckButtonText():
    if varCheckButton.get():
        varLabel.set("checked")
    else:
        varLabel.set("un-checked")  

tkTop = tkinter.Tk()
tkTop.geometry('300x200')

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

varLabel = tkinter.StringVar()
tkLabel = tkinter.Label(textvariable=varLabel)
tkLabel.pack()

varCheckButton = tkinter.IntVar()
tkCheckButton = tkinter.Checkbutton(
    tkTop,
    text="CheckButton",
    variable=varCheckButton,
    command=setCheckButtonText)
tkCheckButton.pack(anchor=tkinter.CENTER)

tkinter.mainloop()