本文实例展示了python tkinter实现简单布局的方法,示例中备有较为详尽的注释,便于读者理解。分享给大家供大家参考之用。具体如下:
# -*- coding: utf-8 -*-
from tkinter import *
root = tk()
# 80×80代表了初始化时主窗口的大小,0,0代表了初始化时窗口所在的位置
root.geometry(’80×80+10+10′)
# 填充方向
”’
label(root, text = ‘l1’, bg = ‘red’).pack(fill = y)
label(root, text = ‘l2’, bg = ‘green’).pack(fill = both)
label(root, text = ‘l3’, bg = ‘blue’).pack(fill = x)
# 左右布局
label(root, text = ‘l1’, bg = ‘red’).pack(fill = y, side = left)
label(root, text = ‘l2’, bg = ‘green’).pack(fill = both, side = right)
label(root, text = ‘l3’, bg = ‘blue’).pack(fill = x, side = left)
# 绝对布局
l4 = label(root, text = ‘l4′)
l4.place(x = 3, y = 3, anchor = nw)
”’
# grid 网格布局
l1 = label(root, text = ‘l1’, bg = ‘red’)
l2 = label(root, text = ‘l2’, bg = ‘blue’)
l3 = label(root, text = ‘l3’, bg = ‘green’)
l4 = label(root, text = ‘l4’, bg = ‘yellow’)
l5 = label(root, text = ‘l5’, bg = ‘purple’)
l1.grid(row = 0, column = 0)
l2.grid(row = 1, column = 0)
l3.grid(row = 1, column = 1)
l4.grid(row = 2 )
l5.grid(row = 0, column = 3)
root.mainloop()
grid 网格布局运行效果如下图所示:
感兴趣的读者可以测试一下本文实例运行效果,相信对大家的python程序设计有一定的借鉴价值。