tkinter的控件定位(位置和容器)
摘要:本文主要介绍控件定位相关的命令:pack,place,grid,Frame,LabelFrame,Toplevel
00 pack() 的使用
参数:padx,pady,anchor,side,fill
from tkinter import *
win=Tk()
win.title('leslie-wj')
win.geometry('500x200')
b1=Button(win,text='aa')
b1.pack(side='left',anchor='n')
b2=Button(win,text='bb')
b2.pack(side='right',anchor='s')
win.mainloop()
b1=Button(win,text='aa',background='yellow')
b1.pack(anchor='n')
b2=Button(win,text='bb',background='green')
b2.pack(side='bottom',fill='x')
win.mainloop()
01 grid() 的使用
参数:padx,pady,row,column,rowspan,columnspan,sticky
from tkinter import *
win=Tk()
win.title('leslie-wj')
win.geometry('500x200')
b1=Label(win,text='aa',background='yellow')
b2=Label(win,text='bb',background='green')
b3=Label(win,text='cc',background='blue')
b1.grid(row=0,column=0)
b2.grid(row=1,column=1)
b3.grid(row=2,column=0)
win.mainloop()
02 place() 的使用
参数:x,y,relx,rely,width,height,relwidth,relheight
from tkinter import *
win=Tk()
win.title('leslie-wj')
win.geometry('500x200')
b1=Label(win,text='aa',background='yellow')
b2=Label(win,text='bb',background='green')
b3=Label(win,text='cc',background='blue')
b1.place(relx=0.1,rely=0.1)
b2.place(relx=0.2,rely=0.2)
b3.place(relx=0.7,rely=0.5)
win.mainloop()
03 Frame容器的使用
from tkinter import *
win=Tk()
win.title('leslie-wj')
win.geometry('500x200')
f1=Frame(win,background='yellow')
f1.pack()
b2=Label(f1,text='bb',background='green')
b2.pack(padx=50,pady=10)
b3=Label(f1,text='cc',background='blue')
b3.pack(anchor='nw')
win.mainloop()
04 LabelFrame容器的使用
from tkinter import *
win=Tk()
win.title('leslie_wj')
win.geometry('500x200')
f1=LabelFrame(win,background='yellow',text='wjwj')
f1.pack()
b2=Label(f1,text='bb',background='green')
b2.pack(padx=50,pady=10)
b3=Label(f1,text='cc',background='blue')
b3.pack(anchor='nw')
win.mainloop()
05 Toplevel的使用