# -*- coding:utf-8 -*- #place 方法 """place()方法内的 x和y 参数可直接设定窗口组件的左上方位置,单位是像素 lab1.place(x=0,y=0) lab2.place(x=30,y=30) """ #width、height参数 """ 有时候在设计窗口应用程序时,所预留的空间有限,如果想要将图片插入窗口内,却担心图片太大,可以在插入图片时同时设定图片的大小,此时可以使用width/height参数,这两个参数可以直接 设定Widget控件的实体大小 from tkinter import * root=Tk() root.title("Myworld") root.geometry("640x480") night=PhotoImage(file="night.png") lab1=Label(root,image=night) lab1.place(x=20,y=30,width=200,height=120) snow=PhotoImage(file="snow.png") lab2=Label(root,image=snow) lab2.place(x=200,y=200,width=400,height=240) root.mainloop() """ # relx rely可以设置相对于父容器(可想成父窗口)的位置,relwidth/relhight设置相对大小。这个相对位置与相对大小是相对于父窗口而言,其值为0.0~1.0 from tkinter import * root=Tk() root.title("MyBeautifulGirl") root.geometry("640x480") night=PhotoImage(file="night.png") label=Label(root,image=night) #label.place(relx=0.1,rely=0.1,relwidth=0.8,relheight=0.8) label.place(relx=0.1,rely=0.1,relheight=0.9) root.mainloop() """ #widgt 控件位置总结 我们使用 tkinter模块设计GUI程序时,虽然可以使用 Place()方法很精确地设置控件的位置,不过笔者建议尽量使用 pack()和grid()方法定位组件,因为当窗口中组件较多时,使用Place 计算组件位置较不方便。同时若有新增或减少组件时又须重新计算组件位置,这样会比较不方便。 """
Tkinter中精确定位的Place方法介绍
最新推荐文章于 2025-06-20 22:47:45 发布