Python.use(better, Tkinter=”GoF”) # Composite

記事一覧 Python.use(better, Tkinter=”GoF")《Python3.1》
# Python で学ぶ デザインパターンの世界

Composite

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

概要

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

事例:step 1

次のコードを実行すると、ウィンドウが現われます。

>>> tips()


## ---------------------------------------- Composite::Component
class Component(object):
    def __init__(self, item):
        self.item = item

    ## ----------------------------------------
    def add_(self, component): pass
    def remove_(self): pass

## ---------------------------------------- Composite::Leaf
class Leaf(Component):
    def __repr__(self):
        return "#%r"%self.item

## ---------------------------------------- Composite::Composite
class Composite(Component):
    def __init__(self, item):
        super().__init__(item)
        self.children = []

    def __repr__(self):
        return "%s(#%s: [%s])"%(
            self.__class__.__name__,
            self.item,
            ",".join(["%s"%e for e in self.children]),
            )

    ## ----------------------------------------
    def add_(self, component):
        self.children.append(component)

## ---------------------------------------- Composite::Client
class TIPS(object):     # Client

    def __init__(self, master, path):
        s = path.split("/")[-1]
        self.tree = Composite(s)
        
        self.folder_image = PhotoImage(file="_image/folder.gif")
        self.file_image   = PhotoImage(file="_image/file.gif")
        self.master = master
        self.path = path

    ## ----------------------------------------
    def createNodes(self):
        self._createNodes(self.tree, self.path)

    def _createNodes(self, parent, path, indent=0):
        tab        = "    "
        tab_folder = "  + "
        tab_file   = "  . "

        def tail(path):
            return path.split("/")[-1]

        for e in listdir(path):
            current = join(path, e)
            if isdir(current):
                component = Composite(e)
                parent.add_(component)
                self.createNode(
                    master=self.master,
                    tab=tab*(indent)+tab_folder,
                    text=tail(current),
                    image=self.folder_image,
                    )
                self._createNodes(component, current, indent+1)
            else:
                component = Leaf(e)
                parent.add_(component)
                self.createNode(
                    master=self.master,
                    tab=tab*(indent)+tab_file,
                    text=tail(current),
                    image=self.file_image,
                    )

    ## ----------------------------------------
    def createNode(self, master, tab, text, image):
        bg   = "yellow"
        font = "courier 12"
        side = LEFT

        frame = Frame(master)
        frame.config(bg=bg)
        frame.pack(anchor=W)
        Label (frame, text=tab , font=font).pack(side=side)
        Button(frame, image=image)         .pack(side=side)
        Label (frame, text=text, font=font).pack(side=side)

## ----------------------------------------
def tips():
    root = Tk()
    root.title("Composite")
    root.config(padx=12, pady=4)

    path = "Python-3.0/Mac/Tools"
    tips = TIPS(root, path)
    tips.createNodes()

    root.mainloop()

TOP


関連記事

Last updated♪2009/10/30