Python.use(better,Tkinter)《59》事例:クイックリファレンスツール(2)
|記事一覧|《こちらに移動中です》2007年4月26日 (木)
Python.use(better, Tkinter)
事例:クイックリファレンスツール(2)《Python3.1》
《著》森こねこ・小粒ちゃん+∞《監修》小泉ひよ子とタマゴ倶楽部
第1版♪2006/10/28
■ 概要
Text 部品の機能を紹介するとともに、アプリケーションのために作成したコードの断片を示します。
Tkinter によるオブジェクト指向プログラミングへの扉を開きます。
※ Tcl/Tk で作成した例題を、Tkinter で再構成しました。
〓 事例:クイックリファレンスツール(2)
root = Tk() root.config(width=420, height=300) root.propagate(False) HelpText( subject=Text, master=root, font="courier 12", file="text/helpText.txt") root.mainloop()
新たに作成した HelpText を利用します。
- subject= には、リファレンス情報を参照したい対象を指定します。ここでは、Text 部品の情報を参照します。
- file= には、作業用ファイルを指定します。この中に、組み込み関数 help の出力結果を残します。
class HelpText: def __init__(self, subject, master, font, file): self.frame = self._init_frame(subject, master) self.dirText = self._init_dirText( subject, self.frame, font) self.helpText = self._init_helpText( subject, self.frame, font, file) def _init_frame(self, subject, root): root.title("Help on class %s"%subject) frame = Frame(root) frame.rowconfigure(0, weight=1) frame.rowconfigure(1, weight=2) frame.columnconfigure(0, weight=1) frame.pack(fill=BOTH, expand=True) return frame def _init_dirText(self, subject, frame, font): parent = ScrollableText(frame, font) parent.frame.grid(row=0, column=0, sticky=NSEW) text = parent.text text.config(wrap=CHAR) self.header_text(text) self.dir_text(text, subject) self.footer_text(text) return text
_init_dirText は、上段のテキスト領域を初期設定します。
- header_text では、キーワードの頭文字を設定します(先頭行)
- dir_text では、組み込み関数 dir で得られる項目を設定します
- footer_text では、矢印ボタンを設定します(末尾行)
def header_text(self, text): def tagNames(index, bg, sq): for i, e in enumerate(range(ord("A"), ord("Z")+1)): c = chr(e) text.insert(END, "%s,"%c) text.tag_config(tagName=c, background=bg) text.tag_bind( tagName=c, sequence=sq, func=lambda e: func(e, index), ) text.tag_add(c, "1.%i"%(i*2), "1.%i"%(i*2+1)) text.insert(END, "\n")
ここでは《55》と違って、各項目を追加挿入 insert するとともに、個別のタグ情報を設定 tag_bind します。
... def func(e, index): s = e.widget.get(index1=CURRENT) match = text.search( pattern="'%s"%s.lower(), index=index, stopindex=END, ) if match: text.see(index=match) tagNames(index="2.0", bg="cyan", sq="")
- get(self,index1,index2=None) は、位置 index1= と index2=(これは含まれない)との間にあるテキストを獲得します。ここで CURRENT を指定すると、選択している位置を指します。
- see(self,index) は、位置 index= にある文字を、テキスト領域の中央に表示(スクロール)します。
〓 関連記事
- INDEX《Tkinter》
- The Tkinter Text Widget ¶
- 24. The Text widget ¶
- The Tkinter Scrollbar Widget ¶
- 22. The Scrollbar widget ¶
- Python.use(better,Tkinter)《余録》Listbox/Scrollbar - 続・ひよ子のきもち
Last updated♪2009/09/09