Python.use(better,Tkinter)《46》Entry

記事一覧

Python.use(better, Tkinter)
Entry《Python3.1》

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

概要

エントリーに入力した文字列を獲得する方法を紹介します。

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

事例1:

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

>>> ex1()


def ex1():
    root = Tk()
    root.title("Entry")
    root.config(padx=16, pady=8)

    for e in "NEWS":
        w = Entry(root)
        w.insert(END, e)
        w.pack()

    root.mainloop()

文字列 "NEWS" を構成する各文字 e を含む、4つのエントリーが表示されます。

《Note》

class Entry(Widget):        # /Python-3.0/Lib/tkinter/__init__.py
    """Entry widget which allows to display simple text."""
    def __init__(self, master=None, cnf={}, **kw):
  • (単一行の)テキストを表示する入力フィールドを提供します。
class Entry(Widget):        # /Python-3.0/Lib/tkinter/__init__.py
    def insert(self, index, string):
        """Insert STRING at INDEX."""
keyword arguments
index= 挿入する位置を指定します
string= 文字列(str)を指定します

事例2:

事例1と同じものを、クラスを使って実現します。

def ex1a():
    class TIPS(object):
        def __init__(self, master):
            for e in "NEWS":
                widget = Entry(master=master)
                widget.insert(END, e)
                widget.pack()

    ## ----------------------------------------
    root = Tk()
    root.title("Entry")
    root.config(padx=16, pady=8)
    TIPS(master=root)
    root.mainloop()

関連記事

Last updated♪2009/09/18