Tkinter 之Grid布局
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()
赞 (0)