Python.use(better,Tkinter)《余録》Example 30-4

記事一覧

Python.use(better, Tkinter)
Example 30-4 《Python3.1》

《著》森こねこ・小粒ちゃん+∞《監修》小泉ひよ子とタマゴ倶楽部
第0版♪1993/11/25 ● 第1版♪2006/10/28

概要

Tkinter によるオブジェクト指向プログラミングへの扉を開きます。
※ Tcl/Tk で作成した例題を、Tkinter で再構成しました。

〓 Example 30-4:Python

Tcl/Tk 版と同等のコードを Python で記述すると、次のようになります。

>>> ex_30_4()


from tkinter import *

def ShowChoices(parent, varname, args):
    f = Frame(parent, borderwidth=5)
    for item in args:
        b = Radiobutton(f,
            variable=varname, text=item, value=item)
        b.pack(side=LEFT)
    f.pack(side=TOP)

def ShowBooleans(parent, args):
    f = Frame(parent, borderwidth=5)
    for item, isSelected in args.items():
        b = Checkbutton(f,
            text=item, variable=item)
        if isSelected: b.select()
        b.pack(side=LEFT)
    f.pack(side=TOP)

def ex_30_4():
    root = Tk()
    root.title("Ex30-4: Radiobuttons and checkbuttons")

    choice = StringVar()
    choice.set("kiwi")

    ShowChoices(root, choice, [
        "apple", "orange", "peach", "kiwi", "strawberry",
        ])
    ShowBooleans(root, {
        "Bold"     : True ,
        "Italic"   : True ,
        "Underline": False,
        })
    
    root.mainloop()

〓 Example 30-4:Tcl/Tk 版

Practical Programming in Tcl and Tk

Practical Programming in Tcl and Tk

この参考文献では、次の事例を紹介しています。

#
# Example 30-4
# Radiobuttons and checkbuttons.
#

proc ShowChoices { parent varname args } {
	set f [frame $parent.choices -borderwidth 5]
	set b 0
	foreach item $args {
		radiobutton $f.$b -variable $varname \
			-text $item -value $item
		pack $f.$b -side left
		incr b
	}
	pack $f -side top
}
proc ShowBooleans { parent args } {
	set f [frame $parent.booleans -borderwidth 5]
	set b 0
	foreach item $args {
		checkbutton $f.$b -text $item -variable $item
		pack $f.$b -side left
		incr b
	}
	pack $f -side top
}
set choice kiwi
ShowChoices {} choice apple orange peach kiwi strawberry
set Bold 1 ; set Italic 1
ShowBooleans {} Bold Italic Underline

Last updated♪2009/09/05