Tkinter 之Place布局
一、参数说明
| 参数 | 作用 |
|---|---|
| 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()
三、效果图


浙公网安备 33010602011771号