Python Tkinter教程(三)——三种几何布局管理器Pack、Place和Grid的所有参数及相关方法及详细用法

简介: Python Tkinter教程(三)——三种几何布局管理器Pack、Place和Grid的所有参数及相关方法及详细用法

一、Pack方法

【Pack方法官方原文:pack manual page - Tk Built-In Commands

【参数说明】f198d7cc86d8487fa240cedc481d7689.pngimage.png【1】如果容器窗口太大,以至于在其所有内容pack后会留下额外的空间,那么额外的空间将均匀分布在为其设置了-expand选项的所有内容中。

【相关方法】image.png【几何传播】Pack通常会计算一个容器控件(如Frame控件)必须有多大才能完全满足其内容物的需要,并将容器控件的宽度和高度设置为这些尺寸。这会导致几何信息通过窗口层次向上传播到顶层窗口(Toplevel类型的控件),以便整个子树自行调整大小以满足叶窗口的需要。但是,pack_propagate命令可用于关闭一个或多个容器控件的传播。如果传播被禁用,则Pack几何布局管理器将不会设置pack所需的宽度和高度。例如,如果希望容器控件具有指定的固定大小,这可能很有用。


【这里推荐一篇写的很好的关于Pack的文章】


用tkinter.pack设计复杂界面布局_快乐清风客的博客-CSDN博客_tkinter界面设计


【代码示例】

from tkinter import *
root = Tk()
root.title('Pack方法')
root.geometry('600x600+300+100')
Frame(root,bg='red',width=100).pack(side='left',fill='y',expand='no')
Frame(root,bg='yellow',height=100).pack(side='top',fill='x')
Frame(root,bg='green').pack(expand=1,fill='both')
root.mainloop()

【运行效果】

二、Place方法

【Place方法官方原文:place manual page - Built-In Commands

【参数说明】

image.png【1】该参数不常用,要参考的可以看一下官方说明:


(我就不翻译了,嘻嘻,主要是没看太懂,有懂的可以在评论区说一下哦!)


-bordermode mode


whether to take border width of master widget into account


Mode determines the degree to which borders within the container are used in determining the placement of the content. The default and most common value is inside. In this case the placer considers the area of the container to be the innermost area of the container, inside any border: an option of -x 0 corresponds to an x-coordinate just inside the border and an option of -relwidth 1.0 means window will fill the area inside the container's border.


If mode is outside then the placer considers the area of the container to include its border; this mode is typically used when placing window outside its container, as with the options -x 0 -y 0 -anchor ne. Lastly, mode may be specified as ignore, in which case borders are ignored: the area of the container is considered to be its official X area, which includes any internal border but no external border. A bordermode of ignore is probably not very useful.

【相关方法】image.png【代码示例】

from tkinter import *
root = Tk()
root.title('Place方法')
root.geometry('600x600+300+100')
Frame(root,bg='red').place(width=450,height=150)
Frame(root,bg='yellow').place(width=150,height=450,x=450)
Frame(root,bg='blue').place(width=450,height=150,x=150,y=450)
Frame(root,bg='green').place(width=150,height=450,y=150)
root.mainloop()

【运行效果】

三、Grid方法

【Grid方法官方原文:grid manual page - Tk Built-In Commands

【参数说明】

image.png

【相关方法】image.pngimage.png【代码示例】

from tkinter import *
root = Tk()
root.title('Grid方法')
root.geometry('600x600+300+100')
Frame(root,bg='red',width=300,height=300).grid(column=0,row=0)
Frame(root,bg='yellow',width=300,height=300).grid(column=0,row=1)
Frame(root,bg='blue',width=300,height=300).grid(column=1,row=0)
Frame(root,bg='green',width=300,height=300).grid(column=1,row=1)
root.mainloop()

【运行效果】


【总结一下】

Pack方法比较简单,可以解决不是非常复杂的图形化界面,但是它只能单一地放置,对于网格化的图形化界面,用Pack就比较繁琐;


Place方法十分精确,它要求了很多参数,能够轻松胜任复杂的图形化界面,但是缺陷也很明显,窗口放大或缩小,Place管理的控件并不能随之改变大小及位置;


Grid方法就是网格,字面意思嘛,它能够很好地应付网格化的图形化界面,但对于复杂且规律不明显的界面而言,Grid方法就显得不太行了;


各有各的优缺点,对于不同的程序我们应该选用合适的几何布局管理器,但是给个小建议,尽量不要在同一个程序中使用不同的几何布局管理器,那样容易把自己弄晕,且代码看起来不清晰。我呢,就比较喜欢用Place了,精确控制控件的大小及位置不就和我们小时候搭积木一样吗?简单容易,就是这个位置坐标有点“费头发”......

目录
相关文章
|
2天前
|
XML 程序员 数据格式
豆瓣评分8.6!Python社区出版的Python故事教程,太强了!
Python 是活力四射的语言,是不断发展中的语言。就连使用 Python 多年的行者也不敢说对 Python 的方方面面都了解并可以自由运用,想必读者可能更加无法快速掌握所有重点技巧了。 今天给小伙伴们分享的这份手册是用互动的开发故事来探讨Pyfhonic开发的故事书籍,是一本Python语言详解书籍,由Python的行者根据自身经验组织而成,是为从来没有听说过Python的其他语言程序员准备的一份实用的导学性质的书,笔者试图将优化后的学习体验,通过故事的方式传达给读者。对于零基础的小白来说更建议入门后再来品读。
|
1天前
|
Python
Python模块的创建方法?
【8月更文挑战第18天】Python模块的创建方法?
4 2
|
3天前
|
索引 Python
Python中的find()和count()方法详解
Python中的find()和count()方法详解
|
3天前
|
SQL JSON C语言
Python中字符串的三种定义方法
Python中字符串的三种定义方法
|
4天前
|
Linux iOS开发 MacOS
|
3月前
|
存储 安全 API
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
121 0
|
3月前
|
机器学习/深度学习 数据采集 C++
【Python 基础教程 07】全面掌握Python3数字操作:入门到精通的实用指南
【Python 基础教程 07】全面掌握Python3数字操作:入门到精通的实用指南
114 2
|
3月前
|
Linux 数据库连接 C++
【Python 基础教程 23】Python3 错误与异常处理全面指南:从入门到精通的实用教程
【Python 基础教程 23】Python3 错误与异常处理全面指南:从入门到精通的实用教程
250 0
|
3月前
|
监控 API C语言
【Python 基础教程 22】全面揭秘Python3 os模块:从入门到高级的实用教程指南
【Python 基础教程 22】全面揭秘Python3 os模块:从入门到高级的实用教程指南
190 1
|
3月前
|
存储 前端开发 C++
【Python 基础教程 09】全面掌握Python3列表:从入门到精通的综合教程与实战指南
【Python 基础教程 09】全面掌握Python3列表:从入门到精通的综合教程与实战指南
122 1