tkinter学习——标签pack方法:side参数

使用pack实现组件由上往下排列

from tkinter import *

window=Tk()
window.title("这是标题")
lab1=Label(window,text="昨天打豆豆",
           bg="lightyellow",
           width=30)
lab2=Label(window,text="今天打豆豆",
           bg="lightgreen",
           width=30)
lab3=Label(window,text="明天打豆豆",
           bg="lightblue",
           width=30)
lab1.pack()   #默认side=TOP 从上往下排列
lab2.pack()
lab3.pack()
window.mainloop()

使用pack实现组件由左往右排列

from tkinter import *

window=Tk()
window.title("这是标题")
lab1=Label(window,text="昨天打豆豆",
           bg="lightyellow",
           width=30)
lab2=Label(window,text="今天打豆豆",
           bg="lightgreen",
           width=30)
lab3=Label(window,text="明天打豆豆",
           bg="lightblue",
           width=30)
lab1.pack(side=LEFT)#side=LEFT 从左往右排列
lab2.pack(side=LEFT)
lab3.pack(side=LEFT)
window.mainloop()

使用pack实现组件由下往上排列

from tkinter import *

window=Tk()
window.title("这是标题")
lab1=Label(window,text="昨天打豆豆",
           bg="lightyellow",
           width=30)
lab2=Label(window,text="今天打豆豆",
           bg="lightgreen",
           width=30)
lab3=Label(window,text="明天打豆豆",
           bg="lightblue",
           width=30)
lab1.pack(side=BOTTOM)#side=BOTTOM 从下往上排列
lab2.pack(side=BOTTOM)
lab3.pack(side=BOTTOM)
window.mainloop()

使用pack实现组件由右往左排列

from tkinter import *

window=Tk()
window.title("这是标题")
lab1=Label(window,text="昨天打豆豆",
           bg="lightyellow",
           width=30)
lab2=Label(window,text="今天打豆豆",
           bg="lightgreen",
           width=30)
lab3=Label(window,text="明天打豆豆",
           bg="lightblue",
           width=30)
lab1.pack(side=RIGHT)#side=RIGHT 从右往左排列
lab2.pack(side=RIGHT)
lab3.pack(side=RIGHT)
window.mainloop()

使用pack实现组件“众”字形排列

from tkinter import *

window=Tk()
window.title("这是标题")
lab1=Label(window,text="昨天打豆豆",
           bg="lightyellow",
           width=30)
lab2=Label(window,text="今天打豆豆",
           bg="lightgreen",
           width=30)
lab3=Label(window,text="明天打豆豆",
           bg="lightblue",
           width=30)
lab1.pack() #默认side=TOP 从上往下排列
lab2.pack(side=LEFT)
lab3.pack(side=RIGHT)
window.mainloop()

使用pack实现组件之间给标签间距排列

from tkinter import *

window=Tk()
window.title("这是标题")
lab1=Label(window,text="昨天打豆豆",
           bg="lightyellow",
           width=30)
lab2=Label(window,text="今天打豆豆",
           bg="lightgreen",
           width=30)
lab3=Label(window,text="明天打豆豆",
           bg="lightblue",
           width=30)
lab1.pack(fill=X)   #再加pady=10就会不一样间距
lab2.pack(pady=10)
lab3.pack(fill=X)
window.mainloop()

使用pack实现组件之间给标签间距左右排列

from tkinter import *

window=Tk()
window.title("这是标题")
lab1=Label(window,text="昨天打豆豆",
           bg="lightyellow",
           width=30)
lab2=Label(window,text="今天打豆豆",
           bg="lightgreen",
           width=30)
lab3=Label(window,text="明天打豆豆",
           bg="lightblue",
           width=30)
lab1.pack(padx=30) #再加pady=10就会不一样间距
lab2.pack(padx=30)
lab3.pack(padx=30)
window.mainloop()

使用pack实现组件之间给横向标签间距排列

from tkinter import *

window=Tk()
window.title("这是标题")
lab1=Label(window,text="昨天打豆豆",
           bg="lightyellow",
           width=30)
lab2=Label(window,text="今天打豆豆",
           bg="lightgreen",
           width=30)
lab3=Label(window,text="明天打豆豆",
           bg="lightblue",
           width=30)
lab1.pack(side=LEFT) #再加padx=10就会不一样间距
lab2.pack(side=LEFT,padx=30)
lab3.pack(side=LEFT)
window.mainloop()

你可能感兴趣的:(Python,tkinter,tkinter,python)