Tkinter place relx/rely and relwidth/relheight parameter
- relx/rely sets the position relative to the parent container (think of it as a parent window).
- relwidth/relheight sets the relative size. This relative position and relative size is relative to the parent window and its value is 0.0 ~ 1.0.
Tkinter place relx/rely and relwidth/relheight Example#1
Place the image night.png from the relative position (0.1,0.1), the relative size is (0.8,0.8)
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("800x400")
night = PhotoImage(file="apidemos-2.png")
label = Label(root,image=night)
label.place(relx=0.1,rely=0.1,relwidth=0.8,relheight=0.8)
root.mainloop()
Output:
At design time, if a certain relative size of a parameter is not set (maybe relwidth or relheight), the unset part will be displayed at the actual size, and you may need to enlarge the window width to display it at that time.
Tkinter place relx/rely and relwidth/relheight Example#2
No relwidth parameter is set.
from tkinter import *
root = Tk()
root.title("apidemos.com")
# root.geometry("1280x800")
root.geometry("128x400")
night = PhotoImage(file="apidemos-2.png")
label = Label(root,image=night)
label.place(relx=0.1,rely=0.1,relheight=0.8)
root.mainloop()
Output:
Part of the right image is not displayed.