欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python Tkinter简单布局实例教程

程序员文章站 2024-01-27 21:33:34
本文实例展示了python tkinter实现简单布局的方法,示例中备有较为详尽的注释,便于读者理解。分享给大家供大家参考之用。具体如下: # -*- codin...

本文实例展示了python tkinter实现简单布局的方法,示例中备有较为详尽的注释,便于读者理解。分享给大家供大家参考之用。具体如下:

# -*- coding: utf-8 -*-
from tkinter import *

root = tk()
# 80x80代表了初始化时主窗口的大小,0,0代表了初始化时窗口所在的位置
root.geometry('80x80+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 Tkinter简单布局实例教程

感兴趣的读者可以测试一下本文实例运行效果,相信对大家的python程序设计有一定的借鉴价值。