Python tkinter library pack layout method call

Python's tkinter library pack layout method is called.
This layout is simple

tkinter.Label(window,text='今晚去庆祝',bg="back",fg='white').pack()
#等同于
label = tkinter.Label(window,text="今晚去庆祝",bg='back',fg='white')
label.pack()

The import mode is import tkinter
to add the name of the imported module to the calling function

import tkinter
window =tkinter.Tk()
window.title("水平居中")
window.geometry("200x100")
window["background"]='white'
tkinter.Label(window,text='烧烤',bg="red",fg='white').pack()
tkinter.Label(window,text="烤鱼",bg='green',fg='black').pack()
tkinter.Label(window,text='雪糕',bg='pink',fg='yellow').pack()
tkinter.mainloop()

The import mode is from tkinter import *
no need to add the module name to the calling function

from tkinter import *
window= Tk()
window.title("dalaojun")
# 设置宽高
window.geometry("500x500")
# 设置背景颜色
window["background"]='white'
# 设置Label控件
# Label(<window窗口对象><text字符串文本><bg背景颜色><fg字体颜色>).pack()调用pack方法布局
Label(window,text="今晚烧烤庆祝",bg='green',fg='black').pack()
Label(window,text='今晚烧烤庆祝',bg='pink',fg='yellow').pack()
# 水平填充 pack(fill=X)
# 通过pack方法的fill关键字将参数值设为'x',可以让控件水平填充 (背景颜色填满了,文字是居中的)
Label(window,text="今晚烧烤庆祝",bg='green',fg='black').pack(fill=X)
Label(window,text='今晚烧烤庆祝',bg='pink',fg='yellow').pack(fill=X)
# 水平外边距
# 通过pack方法padx关键字参数设置控件的水平外边距 padx=10 为水平边距10像素
Label(window,text="今晚烧烤庆祝",bg='green',fg='black').pack(fill=X,padx=10)
Label(window,text='今晚烧烤庆祝',bg='pink',fg='yellow').pack(fill=X,padx=10)
# 垂直外边距
# 通过过pack方法pady关键字参数设置控件的垂直外边距 pady=10 为垂直边距10像素
Label(window,text="今晚烧烤庆祝",bg='green',fg='black').pack(fill=X,padx=10,pady=10)
Label(window,text='今晚烧烤庆祝',bg='pink',fg='yellow').pack(fill=X,padx=10,pady=10)
# 水平内边距
# 通过过pack方法ipadx关键字参数设置控件的水平内边距   这可可能你看不出来
Label(window,text="今晚烧烤庆祝",bg='green',fg='black').pack(fill=X,padx=10,pady=10,ipadx=10)
Label(window,text='今晚烧烤庆祝',bg='pink',fg='yellow').pack(fill=X,padx=10,pady=10,ipadx=10)
# 垂直内边距
# 通过过pack方法ipady关键字参数设置控件的水平内边距 
Label(window,text="今晚烧烤庆祝",bg='green',fg='black').pack(fill=X,padx=10,pady=10,ipadx=10,ipady=10)
Label(window,text='今晚烧烤庆祝',bg='pink',fg='yellow').pack(fill=X,padx=10,pady=10,ipadx=10,ipady=10)
# 水平排列
# 通过pack方法side关键字参数设置多个控件按水平方向(从左向右)(从右向左)排列
# 从左向右排列
Label(window,text="今晚烧烤庆祝01",bg='green',fg='black').pack(padx=10,pady=10,ipadx=10,ipady=10,side=LEFT)
Label(window,text='今晚烧烤庆祝02',bg='pink',fg='yellow').pack(padx=10,pady=10,ipadx=10,ipady=10,side=LEFT)
# 从右向左排列
Label(window,text="今晚烧烤庆祝01",bg='green',fg='black').pack(padx=10,pady=10,ipadx=10,ipady=10,side=RIGHT)
Label(window,text='今晚烧烤庆祝02',bg='pink',fg='yellow').pack(padx=10,pady=10,ipadx=10,ipady=10,side=RIGHT)
#调用mainloop()函数进入事件循环
mainloop()

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/115190992
Recommended