tkinter 提供了下列 21 種 GUI 元件
tkinter 提供了下列 21 種 GUI 元件, 稱為 widget (視窗元件) 或 control (控制項), 所謂 widget 乃 Window gadget 之意 : Label (標籤) Button (按鈕) Radiobutton (單選圓鈕) Checkbutton (核取方塊) Entry (文字欄位) Frame (框) LabelFrame (文字框) Listbox (清單方塊) Text (文字框) Message PanedWindow Scrollbar (捲軸) Scale Spinbox Menu (選單) OptionMenu Menubutton (選單按鈕) Canvas (畫布) Image (圖片) Bitmap (點陣圖) Toplevel 下面先來測試其中最常用的 Button (按鈕) 與 Label (標籤) 元件, 把下列程式用記事本存成 test.py : import tkinter as tk root=tk. Tk () #建立視窗容器物件 root.title("Tk GUI") label= tk . Label ( root , text="Hello World!") #建立標籤物件 label. pack() # 將元件放入容器 button= tk . Button ( root , text="OK") button. pack() #將元件放入容器 root.mainloop() 然後在命令提示字元視窗執行 : D:\Python\test>python test.py 在呼叫 pack() 函數做版面管理的話, tkinter 會將元件由上而下水平置中順序排版. 注意, 在建立 GUI 元件時, 第一個參數必須為其容器物件 (此處為 root 變數所代表之視窗), 格式如下 : 元件變數=元件名稱(容器物件變數, [元件選項]) 其他為元件之選項參數, 例如標籤文字 (text), 大小(size), 邊框 (borde...