tkinter / ウィンドウを表示する

参考: いちばんやさしい Python入門教室, pp.153-180
 

import tkinter as tk
import tkinter.messagebox as msg

def onButton():                                        # ボタンが押されたときに実行する函数を定義する。
    text = entryBox.get()                              # エントリーボックスに入力されたテキストを取得する。
    msg.showinfo("Message Box",                        # メッセージボックスを実体化してそのタイトルを指定する。
                 "Button pushed and "+text+" entered") # 何かをメッセージボックスに表示する。
    textBox.insert(tk.END, text + "\n")                # テキストボックスに何かを表示する。tk.END を指定すると末尾に追加される。
    if text == "close":
        root.destroy()                                 # ウィンドウを閉じる。


root = tk.Tk()              # root という名前でウィンドウを実体化する。
root.title("TITLE is here") # ウィンドウのタイトルを指定する。
root.geometry("600x400")    # ウィンドウのサイズを指定する。x はエックス。



label = tk.Label(root,                      # ラベルを実体化する。
                 text="Enter any number:",  # その文字列を指定する。
                 font=("Courier New", 14))  # そのフォントを指定する。
label.place(x=5, y=20)                      # その位置を指定する。

entryBox = tk.Entry(width=14) # エントリーボックスを実体化する。
entryBox.place(x=5, y=50)     # その位置を指定する。


button = tk.Button(root,                     # ボタンを実体化する。
                   text="Push!",             # ボタンに表示する文字列を指定する。
                   font=("Courier New", 14), # そのフォントを指定する。
                   command=onButton)         # ボタンが押されたときに実行する函数を指定する。
button.place(x=5, y=100)                     # ボタンの位置を指定する。

textBox = tk.Text(root,                          # テキストボックスを実体化する。
                  font=("Courier New", 14))      # そのフォントを指定する。
textBox.place(x=5, y=200, width=200, height=100) # その位置と大きさとを指定する。


root.mainloop()

f:id:ti-nspire:20180109144457p:plain:w400