Friday, December 18, 2015

Python Tkinter: ttk.LabelFrame



#for Python 2
#import Tkinter as tk
#import ttk
#for Python 3
import tkinter as tk
from tkinter import ttk

import platform

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

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

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()

lf1 = ttk.LabelFrame(tkTop, text="LabelFrame 1")
lf1.pack(fill="x", expand="yes")

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

rbVar = tk.IntVar()

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

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

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

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

lf2 = ttk.LabelFrame(tkTop, text="LabelFrame 2")
lf2.pack(fill="both", expand="yes")

def cb1Callback():
    varLabel2.set("Checkbutton 1 clicked: " + str(cb1Var.get()))

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

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

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

varLabel2 = tk.StringVar()
tkLabel2 = tk.Label(lf2, textvariable=varLabel2)
tkLabel2.pack()

tk.mainloop()