xia的小窩

一起來coding和碼字吧

0%

視窗控件配置管理

有三種方式,分為packgridplace

pack

雖然稱之為pack()方法,旦它其實只是在tkinker裡的一個類別

side參數

1
2
3
4
5
label_1 = Label(root, text = "男人的嘴", bg = "lightgreen")
label_2 = Label(root, text = "騙人的鬼", bg = "lightyellow")

label_1.pack(side = BOTTOM)
label_2.pack(side = TOP)

結果

padx / pady 參數

1
2
3
4
5
6
7
label_1 = Label(root, text = "男人的嘴", bg = "lightgreen")
label_2 = Label(root, text = "騙人的鬼", bg = "lightyellow")
label_3 = Label(root, text = "漂亮的女人都愛騙人", bg = "lightyellow")

label_1.pack(padx = 50)
label_2.pack(pady = 100)
label_3.pack(fill = X)

結果

ipadx / ipady 參數

1
2
3
label_1.pack(ipadx = 50)
label_2.pack(ipady = 100)
label_3.pack(fill = X)

結果

anchor 參數 (位置)

1
label_2.pack(anchor = SE)

結果

fill 參數

1
label_1(fill = X)

expand 參數

1
label_2.pack(fill = BOTH, expand = True)
  • 結果

pack方法

1
print(root.pack_slaves())
  • 結果

grid

grid底下還有row / column

row / column

1
2
3
4
5
# label_3 = Label(root, text = "漂亮的女人都愛騙人", bg = "lightblue")

label_1.grid(row = 0, column = 0)
label_2.grid(row = 1, column = 0)
label_3.grid(row = 1, column = 1)

結果

columnspan 參數(標籤占用)

1
2
3
4
5
6
7
8
9
#label_1 = Label(root, text = "男人的嘴", bg = "lightgreen")
#label_2 = Label(root, text = "騙人的鬼", bg = "lightyellow")
#label_3 = Label(root, text = "漂亮的女人都愛騙人", bg = "lightblue")
label_4 = Label(root, text = "所以妳們都很醜", bg = "lightyellow")

label_1.grid(row = 0, column = 0)
label_2.grid(row = 0, column = 1)
label_3.grid(row = 1, column = 0, columnspan = 3)
label_4.grid(row = 1, column = 1)

結果

總的來說,就是直接占空間,如果沒有設定好就會如上圖,被下一個設定值壓過去

rowspan 參數

1
label_3.grid(row = 1, column = 0, rowspan = 6) 

結果

padx / pady 參數

1
2
label_3.grid(row = 1, column = 0, padx = 5) 
label_4.grid(row = 1, column = 1, padx = 5, pady = 10)

結果

sticky參數(類似anchor參數)

1
2
3
4
5
label_1.grid(row = 0, column = 0, padx = 10, sticky = W)
label_2.grid(row = 0, column = 1, sticky = W)
label_3.grid(row = 1, column = 0)
label_4.grid(row = 1, column = 1, padx = 5, pady = 10)
# 嘗試把sticky參數拿掉,就樣就可以看到變化

結果

grid( )應用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from tkinter import *


root = Tk()
root.title("python-tkinter")
root.geometry("300x400")

colors = ["red", "green", "yellow", "pink", "blue", "black"]
r = 0
for color in colors:
Label(root, text = color, relief = "groove", width = 20).grid(row = r, column = 0)
Label(root, bg = color, relief = "ridge", width = 20).grid(row = r, column = 1)
r += 1

root.mainloop()

調色盤

使用rowconfigure / columnconfigure

1
2
3
4
5
6
7
8
9
# rowconfigure(0, weight = 1) row 0 的控件當視窗改變大小的縮放比是 1
# columnconfigure(0, weight = 1) column 0....(同上)

root.rowconfigure(1, weight = 1)
root.columnconfigure(0, weight = 1)

label_1.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = W)
label_2.grid(row = 0, column = 1, padx = 5, pady = 5)
label_3.grid(row = 1, column = 0, columnspan = 2, padx = 5, pady = 5, sticky = N+W+S+E)

結果(可以縮放試試看)

place

使用x / y 參數

1
2
3
label_1.place(x = 10, y = 200)
label_2.place(x = 70, y = 200)
label_3.place(x = 150, y = 200)

結果

使用width / height 參數

1
2
3
4
5
label_1 = Label(root, text = "男人的嘴,騙人的鬼", image = Ball, justify = "left", compound = "right")
label_1.place(x = 20, y = 20, width = 200, height = 200)

label_2 = Label(root, text = "男人的嘴,騙人的鬼", image = Ball, justify = "left", compound = "right")
label_2.place(x = 150, y = 300, width = 300, height = 400)

結果

使用relx / rely 與 relwidth / relheight 參數

這邊就是相對父視窗,以相對位置做判斷

1
label_1.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.8)

結果(因為圖太大的緣故……)

可以嘗試把參數拿掉做實驗喔!!