Python3 Tkinter-Pack

2019-03-28  本文已影响0人  zmqqq

1.创建

from tkinter import *

root=Tk()

print(root.pack_slaves())
Label(root,text='pack').pack()
print(root.pack_slaves())

root.mainloop()
图片.png

2.改变大小

from tkinter import *

root=Tk()
root.geometry('80x80+0+0')
print(root.pack_slaves())
Label(root,text='pack').pack()
print(root.pack_slaves())

root.mainloop()
图片.png

3.添加多个组件

from tkinter import *

root=Tk()
root.geometry('80x80+0+0')
print(root.pack_slaves())
for i in range(5):
    Label(root,text='pack'+str(i)).pack()
print(root.pack_slaves())

root.mainloop()
图片.png

4.子组件布局

from tkinter import *

root=Tk()
root.geometry('80x80+0+0')
print(root.pack_slaves())
Label(root,text='pack1',bg='red').pack(fill=Y)
Label(root,text='pack2',bg='blue').pack(fill=BOTH)
Label(root,text='pack3',bg='green').pack(fill=X)
print(root.pack_slaves())

root.mainloop()
图片.png

5.组件布局

from tkinter import *

root=Tk()
root.geometry('80x80+0+0')
print(root.pack_slaves())
Label(root,text='pack1',bg='red').pack(fill=Y,expand=1)
Label(root,text='pack2',bg='blue').pack(fill=BOTH,expand=1)
Label(root,text='pack3',bg='green').pack(fill=X,expand=1)
print(root.pack_slaves())

root.mainloop()
图片.png

6.改变组件布局

from tkinter import *

root=Tk()
root.geometry('80x80+0+0')
print(root.pack_slaves())
Label(root,text='pack1',bg='red').pack(fill=Y,expand=1,side=LEFT)
Label(root,text='pack2',bg='blue').pack(fill=BOTH,expand=1,side=RIGHT)
Label(root,text='pack3',bg='green').pack(fill=X,expand=1,side=LEFT)
print(root.pack_slaves())

root.mainloop()
图片.png

7.组件间距

from tkinter import *

root=Tk()
root.geometry('80x80+0+0')
print(root.pack_slaves())
Label(root,text='pack1',bg='red').pack(fill=Y,expand=1,side=LEFT)
Label(root,text='pack2',bg='blue').pack(fill=BOTH,expand=1,side=RIGHT,padx=10)
Label(root,text='pack3',bg='green').pack(fill=X,expand=1,side=LEFT,pady=10)
print(root.pack_slaves())

root.mainloop()
图片.png
上一篇下一篇

猜你喜欢

热点阅读