place 布局管理器可以通过坐标精确控制组件的位置
适用于一些布局更加灵活的场景。
place()方法的选项
x,y
组件左上角的绝对 坐标(相对于窗口)
非负整数 x 和 y 选项用于设置偏移(像素),如果同时设置 relx(rely) 和 x(y),那么 place 将优先计算 relx 和 rely,然后再实现 x 和 y 指定的偏移值
relx rely
组件左上角的坐标 (相对于父容器)
relx 是相对父组件的位置。0 是最左边,0.5 是正中间,1 是最右边; rely 是相对父组件的位置。0 是最上边,0.5 是正中间,1 是最下边;
width, height
组件的宽度和高度
非负整数
relwid th, relhei
组件的宽度和高度 (相对于父容器)
与 relx、rely 取值类似,但是相对于父组件的尺寸
ght anchor
对齐方式,左对齐” w”,右对齐”e”, 顶对齐”n”,底对 齐”s”
“n”, “s”, “w”, “e”, “nw”, “sw”, “se”, “ne”, “center”(默认)
"""
测试
"""
from tkinter import *
from tkinter import messagebox
import random
class Application(Frame):
def __init__(self, mester=None):
super().__init__(mester)
self.mester = mester
self.pack()
self.creatWidget()
def creatWidget(self):
f1 = Frame(root, width=200, height=200, bg='green')
f1.place(x=60, y=60)
Button(root, text='python').place(relx=0.2, y=80, relwidth=0.3, relheight=0.2)
Button(f1, text='java').place(relx=0.6, rely=0.8)
if __name__ == '__main__':
root = Tk()
root.geometry('400x400+200+200')
app = Application(mester=root)
root.mainloop()
place 布局管理-上下的小狗
"""
测试
"""
from tkinter import *
from tkinter import messagebox
import random
class Application(Frame):
def __init__(self, mester=None):
super().__init__(mester)
self.mester = mester
self.pack()
self.creatWidget()
def creatWidget(self):
# self.photos = [PhotoImage(file='photo/1.gif' + str(i + 1) + '.gif') for i in range(2)]
# self.pukes=[Label(root,image=self.photos[i]) for i in range(2)]
# for i in range(2):
# self.pukes[i].place(x=10+i*40,y=50)
self.photo1 = PhotoImage(file='photo/222.gif')
self.puks01 = Label(root, image=self.photo1)
self.puks01.place(x=10, y=50)
self.photo2 = PhotoImage(file='photo/333.gif')
self.pukse02 = Label(root, image=self.photo2)
self.pukse02.place(x=200, y=50)
self.puks01.bind_class('Label', '<Button-1>', self.chupai)
self.pukse02.bind_class('Label', '<1>', self.chupai)
# 为所有的Label增加事件处理
def chupai(self, event):
print(event.widget.winfo_geometry())
print(event.widget.winfo_y())
if event.widget.winfo_y() == 50:
event.widget.place(y=30)
else:
event.widget.place(y=50)
if __name__ == '__main__':
root = Tk()
root.geometry('600x300+200+200')
app = Application(mester=root)
root.mainloop()
》》》
104x84+200+50
50
104x84+200+30
30
192x152+10+50
50