【Python】Tkinter布局之Pack
推荐指数:四颗星
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
from tkinter import *
root = Tk()
root.title("Tkinter GUI - 控件位置") # 设置标题
root.geometry("300x300+100+100") # 设置窗口大小和起始位置
# 001. 左上下右
# label1 = Label(root, text="left", bg="lightgreen", width=5)
# label1.pack(side="left")
# label2 = Label(root, text="top", bg="lightgreen", width=5)
# label2.pack(side="top")
# label3 = Label(root, text="bottom", bg="lightgreen", width=5)
# label3.pack(side="bottom")
# label4 = Label(root, text="right", bg="lightgreen", width=5)
# label4.pack(side="right")
# 002. 为标签设置pady=5
# label1 = Label(root, text="第一行文本", bg="lightgreen", width=15)
# label1.pack(pady=5)
# label2 = Label(root, text="第二行文本", bg="lightgreen", width=15)
# label2.pack(pady=5)
# 003. 为标签设置ipady=5
# label1 = Label(root, text="第一行文本", bg="lightgreen", width=15)
# label1.pack(pady=5, ipady=5)
# label2 = Label(root, text="第二行文本", bg="lightgreen", width=15)
# label2.pack(pady=5, ipady=5)
# 004. 在窗口右下方建立一个“YES”的标签,一个“NO”标签。其中,标签与窗口左边和上方的间距是10像素。
# label1 = Label(root, text="YES", bg="green", fg="white")
# label1.pack(padx=10, pady=10, ipadx=5, anchor="s", side="right")
# label2 = Label(root, text="NO", bg="red", fg="white")
# label2.pack(padx=10, pady=10, ipadx=5, anchor="s", side="right")
# 005. 第一行文本填满X轴
# label1 = Label(root, text="第一行文本", bg="lightgreen")
# label1.pack(fill="x", pady=5)
# 006. 第一列填满Y轴
# label1 = Label(root, text="第一列文本", bg="lightgreen", relief="solid")
# label1.pack(side="left", fill="y")
# 007. 水平垂直居中显示
# label2 = Label(root, text="Main", bg="lightgreen", relief="solid")
# label2.pack(expand=True)
# 007. 列出执行前后Widget控件中的内容。
# print("执行前:", root.slaves())
# label1 = Label(root, text="YES", bg="green", fg="white")
# label1.pack(padx=10, pady=10, ipadx=5, anchor="s", side="right")
# label2 = Label(root, text="NO", bg="red", fg="white")
# label2.pack(padx=10, pady=10, ipadx=5, anchor="s", side="right")
# print("执行后:", root.slaves())
root.mainloop()