【Python】Tkinter布局之Grid
推荐指数:五颗星
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
from tkinter import *
root = Tk()
root.title("Tkinter GUI - 控件位置") # 设置标题
root.geometry("300x300+100+100") # 设置窗口大小和起始位置
# 001. grid布局:左上下右
# label1 = Label(root, text="左", bg="lightgreen", width=5)
# label1.grid(row=1, column=0)
# label2 = Label(root, text="上", bg="lightgreen", width=5)
# label2.grid(row=0, column=1)
# label3 = Label(root, text="下", bg="lightgreen", width=5)
# label3.grid(row=2, column=1)
# label4 = Label(root, text="右", bg="lightgreen", width=5)
# label4.grid(row=1, column=2)
# 001. grid布局:建议课程表
# label0_0 = Label(root, text="科目", bg="lightgreen")
# label0_0.grid(row=0, column=0)
# label0_1 = Label(root, text="上课", bg="lightgreen")
# label0_1.grid(row=0, column=1, columnspan=5)
# label0_5 = Label(root, text="休息", bg="lightgreen")
# label0_5.grid(row=0, column=5, columnspan=2)
# label1_0 = Label(root, text="星期", bg="lightgreen")
# label1_0.grid(row=1, column=0)
# label1_1 = Label(root, text="星期一", bg="lightgreen")
# label1_1.grid(row=1, column=1)
# label1_2 = Label(root, text="星期二", bg="lightgreen")
# label1_2.grid(row=1, column=2)
# label1_3 = Label(root, text="星期三", bg="lightgreen")
# label1_3.grid(row=1, column=3)
# label1_4 = Label(root, text="星期四", bg="lightgreen")
# label1_4.grid(row=1, column=4)
# label1_5 = Label(root, text="星期五", bg="lightgreen")
# label1_5.grid(row=1, column=5)
# label1_6 = Label(root, text="星期六", bg="lightgreen")
# label1_6.grid(row=1, column=6)
# label1_7 = Label(root, text="星期日", bg="lightgreen")
# label1_7.grid(row=1, column=7)
# label2_0 = Label(root, text="上午", bg="lightgreen")
# label2_0.grid(row=2, column=0, rowspan=2)
# label2_1 = Label(root, text="语文", bg="lightgreen")
# label2_1.grid(row=2, column=1)
# label2_2 = Label(root, text="数学", bg="lightgreen")
# label2_2.grid(row=2, column=2)
# label2_3 = Label(root, text="英语", bg="lightgreen")
# label2_3.grid(row=2, column=3)
# label2_4 = Label(root, text="英语", bg="lightgreen")
# label2_4.grid(row=2, column=4)
# label2_5 = Label(root, text="物理", bg="lightgreen")
# label2_5.grid(row=2, column=5)
# label2_6 = Label(root, text="计算机", bg="lightgreen")
# label2_6.grid(row=2, column=6)
# label2_7 = Label(root, text="休息", bg="lightgreen")
# label2_7.grid(row=2, column=7, rowspan=2)
# label3_1 = Label(root, text="数学", bg="lightgreen")
# label3_1.grid(row=3, column=1)
# label3_2 = Label(root, text="数学", bg="lightgreen")
# label3_2.grid(row=3, column=2)
# label3_3 = Label(root, text="地理", bg="lightgreen")
# label3_3.grid(row=3, column=3)
# label3_4 = Label(root, text="历史", bg="lightgreen")
# label3_4.grid(row=3, column=4)
# label3_5 = Label(root, text="化学", bg="lightgreen")
# label3_5.grid(row=3, column=5)
# label3_6 = Label(root, text="计算机", bg="lightgreen")
# label3_6.grid(row=3, column=6)
# 003. 原则上,相同column但宽度不同时,会保留最宽的控件当作基准,比较短的控件会居中对齐
# label1 = Label(root, text="我比较长", bg="lightgreen")
# label1.grid(row=0, column=0)
# label2 = Label(root, text="我短", bg="lightgreen")
# label2.grid(row=1, column=0)
# 004. 设置为左对齐 sticky=W
# label1 = Label(root, text="我比较长", bg="lightgreen")
# label1.grid(row=0, column=0)
# label2 = Label(root, text="我短", bg="lightgreen")
# label2.grid(row=1, column=0, sticky="w")
# 005. sticky参数组合使用 sticky=W+E 两端对齐
# label1 = Label(root, text="我比较长", bg="lightgreen")
# label1.grid(row=0, column=0)
# label2 = Label(root, text="我短", bg="lightgreen")
# label2.grid(row=1, column=0, sticky="we")
# 006. 设置第一行的缩放比例为1
root.grid_rowconfigure(0, weight=1)
label1 = Label(root, text="我比较长", bg="lightgreen")
label1.grid(row=0, column=0)
label2 = Label(root, text="我短", bg="lightgreen")
label2.grid(row=1, column=0, sticky="we")
root.mainloop()