一、參數說明
| 參數 | 作用 |
|---|---|
| anchor | 控制組件在 place 分配的空間中的位置 "n", "ne", "e", "se", "s", "sw", "w", "nw", 或 "center" 來定位(ewsn代表東西南北,上北下南左西右東) 默認值是 "nw" |
| bordermode | 指定邊框模式("inside" 或 "outside") 默認值是 "inside" |
| height | 指定該組件的高度(像素) |
| in_ | 將該組件放到該選項指定的組件中 指定的組件必須是該組件的父組件 |
| relheight | 指定該組件相對於父組件的高度 取值范圍 0.0 ~ 1.0 |
| relwidth | 指定該組件相對於父組件的寬度 取值范圍 0.0 ~ 1.0 |
| relx | 指定該組件相對於父組件的水平位置 取值范圍 0.0 ~ 1.0 |
| rely | 指定該組件相對於父組件的垂直位置 取值范圍 0.0 ~ 1.0 |
| width | 指定該組件的寬度(像素) |
| x | 指定該組件的水平偏移位置(像素) 如同時指定了 relx 選項,優先實現 relx 選項 |
| y | 指定該組件的垂直偏移位置(像素) 如同時指定了 rely 選項,優先實現 rely 選項 |
二、代碼示例
import tkinter as tk
window = tk.Tk()
# 設置窗口大小
winWidth = 600
winHeight = 400
# 獲取屏幕分辨率
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()
x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)
# 設置主窗口標題
window.title("Place參數說明")
# 設置窗口初始位置在屏幕居中
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# 設置窗口圖標
window.iconbitmap("./image/icon.ico")
# 設置窗口寬高固定
window.resizable(0, 0)
frame = tk.Frame(window)
frame.place(rely=.5, relx=0.5, x=-100, y=-100)
# 返回參數信息
print(frame.place_info())
tk.Label(frame, text="用戶名").grid(row=0)
tk.Label(frame, text="密碼").grid(row=1)
username_var = tk.StringVar()
pwd_var = tk.StringVar()
tk.Entry(frame, textvariable = username_var).grid(row=0, column=1)
tk.Entry(frame, show="*", textvariable=pwd_var).grid(row=1, column=1)
photo = tk.PhotoImage(file = "./image/loading.gif")
tk.Label(frame, image=photo).grid(row = 0, rowspan=2, column=2, padx=5, pady=5)
def login():
username = username_var.get()
password = pwd_var.get()
print("username=%s, password=%s" % (username, password))
tk.Button(frame, text="登錄", command=login, padx=20).grid(row=2, columnspan=3)
window.mainloop()
三、效果圖

