一、參數說明
| 參數 | 作用 |
|---|---|
| column | 指定組件插入的列(0 表示第 1 列) 默認值是 0 |
| columnspan | 指定用多少列(跨列)顯示該組件 |
| row | 指定組件插入的行(0 表示第 1 行) |
| rowspan | 指定用多少行(跨行)顯示該組件 |
| in_ | 將該組件放到該選項指定的組件中 指定的組件必須是該組件的父組件 |
| ipadx | 水平方向上的內邊距 |
| ipady | 垂直方向上的內邊距 |
| padx | 水平方向上的外邊距 |
| pady | 垂直方向上的外邊距 |
| sticky | 控制組件在 grid 分配的空間中的位置 可以使用 "n", "e", "s", "w" 以及它們的組合來定位(ewsn代表東西南北,上北下南左西右東) 使用加號(+)表示拉長填充,例如 "n" + "s" 表示將組件垂直拉長填充網格,"n" + "s" + "w" + "e" 表示填充整個網格 不指定該值則居中顯示選項 含義 |
二、代碼示例
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("Grid參數說明")
# 設置窗口初始位置在屏幕居中
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=-122.5, y=-100, width=245, height=200)
# 返回參數信息
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()
三、效果圖

