Tkinter (Python)

Setup

set window

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# import tkinter
from tkinter import *
# init for window
window = Tk()
# 標題
window.title("My First GUI Program")
# windows size
window.minsize(width=500, height=300)
# padx, pady : window 預留語版邊 pad
# bg : window 背景顏色
window.config(padx=50, pady=70, bg="#B1DDC6")

# 保持顯示 window
window.mainloop()

Setting Options

1
2
3
4
5
6
7
# At object creation time, using keyword arguments
fred = Button(self, fg="red", bg="blue")
# After object creation, treating the option name like a dictionary index
fred["fg"] = "red"
fred["bg"] = "blue"
# Use the config() method to update multiple attrs subsequent to object creation
fred.config(fg="red", bg="blue")

邊線與邊框

1
2
3
# highlightthickness=0 無邊線
# borderwidth=0 無邊框
ok_btn = Button(image=ok_img, command=ok_process, borderwidth=0, highlightthickness=0)

Layout(單一視窗,僅能擇一使用)

Pack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# init 父窗口
root = Tk()
root2 = Tk()

w = Label(root, text="Red Sun", bg="red", fg="white")
# ipadx,ipady 設內部的 padding
w.pack(ipadx=10, ipady=10)
# fill=X 表寬度與父窗口一樣
w = Label(root, text="Green Grass", bg="green", fg="black")
w.pack(fill=X)
# padx, pady 設定與其他元件的 padding(含父窗口)
w = Label(root, text="Blue Sky", bg="blue", fg="white")
w.pack(fill=X, padx=10, pady=10)

# 設定 順序 side=LEFT
w = Label(root2, text="red", bg="red", fg="white")
w.pack(padx=5, pady=10, side=LEFT)
w = Label(root2, text="green", bg="green", fg="black")
w.pack(padx=5, pady=20, side=LEFT)
w = Label(root2, text="blue", bg="blue", fg="white")
w.pack(padx=5, pady=20, side=LEFT)

Grid

Grid 把元件位置作為一個二維表結構來維護, 即按照行列的方式排列

1
2
3
4
5
6
7
8
9
10
11
12
13
# grid 0, 0
my_label = Label(text="I Am a Label")
my_label.grid(row=0, column=0)
# grid 1, 1
button = Button(text="click me")
button.grid(row=1, column=1)
# grid 2, 3
entry = Entry(width=30)
entry.insert(END, "Some text to begin with.")
entry.grid(row=2, column=3)
# grid 0, 2
button2 = Button(text="button2")
button2.grid(row=0, column=2)

Place

Place 可以指定元件的絕對位置

1
2
my_label = Label(text="I Am a Label")
my_label.place(x=50, y=70)

元件

Text

1
2
3
4
5
6
7
8
9
text = Text(width=30, height=5)
text.insert(END, "Example of multi-line text entry.")
text.pack()
# print to console
# index1: 1.0 第一個字元
# index2: 結尾
print(text.get("1.0", END))
# focus
text.focus()

Label

1
2
3
4
5
6
7
8
9
# 設定 back ground and front ground color
my_label = Label(text="I Am a Label", font=("Arial", 24, "bold"), fg='green', bg='yellow')
# side = top(default), left, right, bottom
# expand = True (垂直占最大)
my_label.pack(side="bottom", expand=True)
# 更改 label 內容 - 方法1
my_label["text"] = "New label"
# 更改 label 內容 - 方法2
my_label.config(text="New Text - config")

Button

1
2
3
4
5
6
7
# button's click function
def button_clicked():
print("I got a clicked.")

# button create
button = Button(text="click me", command=button_clicked)
button.pack()

Spinbox

1
2
3
4
5
6
7
8
# Spinbox
def spinbox_used():
# get spinbox value
print(spinbox.get())

spinbox = Spinbox(from_=0, to=10, width=5, command=spinbox_used)
# 留 padding
spinbox.pack(padx=20, pady=10)

Standard Dialog

1
2
3
4
5
6
7
8
9
10
11
# 因不是 class 要另外 import
from tkinter import messagebox

# Standard Dialog
website = "Game App"
email = "example.gmail.com"
password = "123abc"
# Dialog askokcancel
is_ok = messagebox.askokcancel(title=website, message=f"There are the details entered: \nEmail: {email} \nPassword: {password}\n Is it ok to save")
# Dialog showinfo
messagebox.showinfo(title="Oops", message="Please don't leave field empty.")

Radio Box

1
2
3
4
5
6
7
8
9
10
def radio_used():
# get value
print(radio_state.get())

radio_state = IntVar()
radiobutton1 = Radiobutton(text="option1", value=1, variable=radio_state, command=radio_used)
radiobutton2 = Radiobutton(text="option2", value=2, variable=radio_state, command=radio_used)
print(radio_state.get())
radiobutton1.pack()
radiobutton2.pack()

Listbox

1
2
3
4
5
6
7
8
9
10
def listbox_used(event):
print(listbox.get(listbox.curselection()))

listbox = Listbox(height=4)
fruits = ["Apple", "Pear", "Orange", "Banana"]
for item in fruits:
listbox.insert(fruits.index(item), item)
listbox.bind("<<ListboxSelect>>", listbox_used)
# 留 padding
listbox.pack(padx=20, pady=10)

Checkbox

1
2
3
4
5
6
7
8
9
10
11
12
def checkbutton_used():
# get value
print(checked_state.get())

checked_state = IntVar()
# set value to 1
# checked_state.set(1)
# only show type
print(checked_state)
checkbutton = Checkbutton(text="Is on?", variable=checked_state, command=checkbutton_used)
print(checked_state.get())
checkbutton.pack()

Scale

1
2
3
4
5
6
# parameter include value
def scale_used(value):
print(value)

scale = Scale(from_=0, to=100, command=scale_used)
scale.pack()

Entry (單行輸入)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# for get entry value ========
def button_clicked():
print("I got a clicked.")
new_input = entry.get()
print(new_input)
my_label["text"] = new_input

# get entry value
button = Button(text="click me", command=button_clicked)
button.pack()
# =============================


# entry
entry = Entry(width=30)
# 設定預設值
# index-->END : 存在文字之後
entry.insert(END, "Some text to begin with.")
# 留 padding
entry.pack(padx=20, pady=10)
# focus
entry.focus()
# not show - because it run when not input
# get entry value
print(f"-->{entry.get()}")

# delete entry from position to end
# website_entry.delete(0, "end")
# entry.delete(0, "end")
# or
# website_entry.delete(0, END)
# entry.delete(0, END)

Canvas (繪圖)

1
2
3
4
5
6
7
8
9
10
11
12
# constant
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
# Canvas, highlightthickness 邊線寬度
canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0)
# image 元件
tomato_img = PhotoImage(file="tomato.png")
# add image
canvas.create_image(100, 112, image=tomato_img)
# add text, fill 填入顏色
canvas.create_text(102, 130, text="00:00", font=(FONT_NAME, 35, "bold"), fill="white")
canvas.pack()

ttk

Treeview

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from tkinter import *
from tkinter import ttk


window = Tk()
# 標題
window.title("Table Tk")

textSql = Text(height=3, width=100)
textSql.pack()
textSql.insert(END, "SELECT * FROM stockPrice")

button1 = Button(text="查詢")
button1.pack(pady=10)

tree = ttk.Treeview(column=("c1", "c2", "c3", "c4"), show='headings')
tree.column("#1", anchor=CENTER)
tree.heading("#1", text="日期")
tree.column("#2", anchor=CENTER)
tree.heading("#2", text="開盤")
tree.column("#3", anchor=CENTER)
tree.heading("#3", text="收盤價")
tree.column("#4", anchor=CENTER)
tree.heading("#4", text="成交筆數")
tree.pack()

# 保持顯示 window
window.mainloop()

Bind

Listbox

1
2
3
4
5
6
7
8
9
def listbox_used(event):
print(listbox.get(listbox.curselection()))

listbox = Listbox(height=4)
fruits = ["Apple", "Pear", "Orange", "Banana"]
for item in fruits:
listbox.insert(fruits.index(item), item)
listbox.bind("<<ListboxSelect>>", listbox_used)
listbox.pack()

參考