python之place布局管理器

本文详细介绍了Tkinter中Place布局管理器的使用方法,包括如何通过坐标精确控制组件位置,适用于需要灵活布局的场景。文章深入讲解了place()方法的各项参数,如x, y, relx, rely等,并提供了实例代码展示布局效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值