Python | Tkinter各種元件使用(上)

Kombayar
5 min readOct 8, 2020

--

Tkinter是一個GUI套件,跨平台以python為基礎,融入Tcl/Tk GUI目前是python的標準套件。提供十幾種元件,配合元件佈置管理、滑鼠與按鍵式件設計、功能表與對話框應用,來達成程式設計者自己想要的視窗架構。

這邊將介紹部分元件的用途與元件樣式。

1. 視窗元件

title(‘’): 設定視窗標題

geometry(‘width x height’): 設定視窗大小(單位Pixeks)

maxsize(width, height): 設定最大化視窗大小(單位Pixeks)

minsize(width, height): 設定最小化視窗大小(單位Pixeks)

mainloop(): 持續執行視窗元件

destroy(): 關閉視窗元件

2. Frame 框架元件

width: 寬度(單位為Pixels)

height: 高度(單位為Pixels)

bg(background): 背景顏色

bd(borderwidth): 邊框線寬

relief: 框線樣式(flat, sunken, raised, groove, ridge)

3. LabelFrame 標籤框架元件

text: 框架標籤文字

padx: 水平間距(單位為Pixels)

pady: 垂直間距(單位為Pixels)

4. PanedWindow 拼布分塊式可動視窗元件

oriental : 方向(horizontal, vertical)

5. Button 按鈕元件

font: 設定顯示字型("字體, 字型大小, 粗細")

command: 對應要執行的函式(含是要寫在元件前)

anchor: 按鈕上元件對齊方式

bitmap: 使用按鈕圖片

6. Label 標籤元件

7. Entry 單行文字方塊元件

textvariable: 設定文字變數

entry可被作為輸入與輸出,使用.get()取得輸入,使用.set()顯示輸出。

8. Text 多行文字方塊

wrap: 換行(char段字換行, word不斷字換行, none不換行)

state: 輸入狀態(normal正常輸入, disable無法輸入)

.get(start. end): 取得輸入

.insert(index, text): 插入文案

.config(): 重新設定參數,ex:font(“Arial”, 10, “bold”)

.delete(start, end): 刪除文段

9. Scale 滑桿元件

sliderlength: 滑桿長度

sliderrelief: 滑桿樣式(flat, sunken, raised, groove, ridge)

from_: 滑桿初始值

to: 滑桿終值

10. Spinbox 數字選取方塊元件

11.Listbox 列式方塊元件

.insert(index, text): 輸入文字

.curselection(): 選到的物件

12. Radiobutton 選項按鈕元件(單選)

13. CheckButton 核取按鈕元件(多選)

variable: 元件連結變數

第一階段的介紹先到這邊,有興趣的朋友可以繼續查看(下),有更多關於tkinter的用法介紹哦。

--

--

Kombayar
Kombayar

Written by Kombayar

I’m a graduate student. Self-teaching coding on Internet, wanted to write some articles to make myself remember the code.

No responses yet