• Tkinter pack()布局


    Tkinter pack()布局

    pack 管理器是根据我们在代码中添加组件的顺序依次排列所有组件,非常简单方便。

    通常来说,pack 适用于少量组件或简单布局的情况。它不像 place 和 grid 布局管理器需要计算位置(坐标或者行列),pack 只有上下左右的关系,一个个按添加顺序放上去即可。

    1. pack 纵向依次排列

    在默认情况下,使用 pack 方法就是将各个组件按照添加顺序纵向依次排列。

    import tkinter as tk 
    
    root = tk.Tk()
    
    tk.Label(root, text="Red", bg="red", fg="white").pack()
    tk.Label(root, text="Yellow", bg="yellow", fg="black").pack()
    tk.Label(root, text="Green", bg="green", fg="white").pack()
    tk.Label(root, text="Blue", bg="blue", fg="white").pack()
    
    root.mainloop()

    2. pack 横向依次排列

    如果我们希望所有组件按照顺序横向依次排列,我们可以添加参数 side='left',从而就实现了从左往右的横向排列效果。

    import tkinter as tk 
    
    root = tk.Tk()
    
    tk.Label(root, text="Red", bg="red", fg="white").pack(side='left')
    tk.Label(root, text="Yellow", bg="yellow", fg="black").pack(side='left')
    tk.Label(root, text="Green", bg="green", fg="white").pack(side='left')
    tk.Label(root, text="Blue", bg="blue", fg="white").pack(side='left')
    
    root.mainloop()

    3. pack 的复杂布局效果

    我们再来试一试相对比较多的组件下复杂的 pack 布局。

    为了实现整个布局的整齐美观,我们通常会设置fill、expand参数。

    fill 表示填充分配空间
        x:水平方向填充
        y:竖直方向填充
        both:水平和竖直方向填充
        none:不填充(默认值)

    expand 表示是否填充父组件的额外空间
        True 扩展整个空白区
        False 不扩展(默认值)

    expand置True 使能fill属性
    expand置False 关闭fill属性 fill=X 当GUI窗体大小发生变化时,widget在X方向跟随GUI窗体变化 fill=Y 当GUI窗体大小发生变化时,widget在Y方向跟随GUI窗体变化 fill=BOTH 当GUI窗体大小发生变化时,widget在X、Y两方向跟随GUI窗体变化

    import tkinter as tk 
    
    root = tk.Tk()
    
    letter = ['A','B','C','D','E']
    
    for i in letter:
        tk.Label(root, text=i, relief='groove').pack(fill='both', expand=True)
    for i in letter:
        tk.Label(root, text=i, relief='groove').pack(side='left', fill='both', expand=True)
    for i in letter:
        tk.Label(root, text=i, relief='groove').pack(side='bottom', fill='both', expand=True)
    for i in letter:
        tk.Label(root, text=i, relief='groove').pack(side='right',fill='both', expand=True)
     
    root.mainloop()

    三、参数方法

    1. 参数汇总

    布局管理器 pack 涉及的相关参数以及用法。


    原文链接:https://blog.csdn.net/nilvya/article/details/106148018

  • 相关阅读:
    干掉idea视图黄色警告
    idea插件 Background Image Plus 随机更换背景图片
    es的rest风格的api文档
    使用postman对elasticsearch接口调用
    es插件安装
    emoji表情等特殊字符处理和存储的两个方案
    java8两个字段进行排序问题
    java 必应壁纸批量下载
    elasticsearch学习(1)
    RocketMq报错 Java HotSpot(TM) Server VM warning:
  • 原文地址:https://www.cnblogs.com/emanlee/p/15340485.html
Copyright © 2020-2023  润新知