Friday, December 18, 2015

Python Tkinter: Checkbutton



#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 cb1Callback():
    varLabel.set("Checkbutton 1 clicked: " + str(cb1Var.get()))

cb1Var = tk.BooleanVar()
cb1 = tk.Checkbutton(
    tkTop,
    text="Checkbutton 1",
    width = 50,
    background='#B0B0B0',
    anchor=tk.W,
    variable=cb1Var,
    command=cb1Callback)
cb1.pack()

def cb2Callback():
    varLabel.set("Checkbutton 2 clicked: " + cb2Var.get())

cb2Var = tk.StringVar()
cb2 = tk.Checkbutton(
    tkTop,
    text="Checkbutton 2 - ON/OFF",
    width = 50,
    background='#C0C0C0',
    anchor=tk.W,
    variable=cb2Var,
    onvalue="ON",
    offvalue="OFF",
    command=cb2Callback)
cb2.pack()

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

tk.mainloop()