Python.use(better, src=PyPy) #179:《余録》ScrolledList.py

中級篇

Python.use(better, src=PyPy)
ソースコードの歩き方《PyPy1.2》

《著》小粒ちゃん@湘南組、小粒ちゃん@博多組《監修》小泉ひよ子とタマゴ倶楽部

PyPy1.2 のリリースを機に、PyPy1.1 版を再構成しました。
※ compiler の傘下にあるモジュールは、PyPy1.1.0 からの変更はありません。


《余録》ScrolledList.py


クラス ScrolledList を利用した事例を使って、その機能を確認します。ここでは、ScrolledList を利用して、クラス Tk の属性を閲覧できるようにした事例を紹介します。

■ 動作の確認

モジュールを起動すると、次のようなウィンドウが現れます。

$ python cherry_ScrolledList.py

任意の項目を(シングル)クリックすると、

config: <function configure at 0x758bb8>

その識別情報が出力されます。ここでは、選択した項目 config が、関数オブジェクト configure(config は、configure の別名)だと分かります。また、任意の項目をダブルクリックすると、

Help on function configure in module tkinter:

configure(self, cnf=None, **kw)
    Configure resources of a widget.
    
    The values for resources are specified as keyword
    arguments. To get an overview about
    the allowed keyword arguments call the method keys.

そのヘルプ情報が出力されます。ここでは、選択した項目 config/configure が、モジュール tkinter 内にあるのが分かります。

■ コードの解説

クラス Tips は、ScrolledList を拡張して、任意の属性を閲覧できるようにしたものです。

$ cat cherry_py/cherry_ScrolledList.py
...
from tkinter import Tk
from ScrolledList import ScrolledList

def tips(subject):
    root = Tk()
    root.title("dir(%s)"%subject.__name__)
    root.protocol("WM_DELETE_WINDOW", root.destroy)

    class Tips(ScrolledList):
        def on_select(self, index):
            s = self._attribute(index)
            print("%s: %s"%s)
        def on_double(self, index):
            name, value = self._attribute(index)
            help(value)
        def _attribute(self, index):
            name = self.get(index)
            return name, getattr(subject, name)

    s = Tips(root)
    for e in dir(subject):
        s.append(e)

    root.mainloop()

## ----------------------------------------
if __name__ == '__main__':
    tips(Tk)

 ↑ TOP

》作業中です《
Created: 2008/11/25|Last updated: 2010/05/23