如果不调整框架大小以适合图像,则无法设置框架背景图像

Can't set frame background image without frame resizing to fit image

我可以设置主要 window 的背景,而且我不需要担心 window 试图适应背景图像或图像将小部件推开.

然而,当我尝试对一个框架做同样的事情时,它变得一团糟...

我已经尝试了一些东西,但一切都与调整图像本身的大小有关,我不想扭曲图像。我希望图像位于框架中,而不是调整框架大小以适合图像。

有没有办法在不改变框架大小的情况下将图像放入框架的背景中?

编辑:注意:我使用的图像大到足以填满屏幕,因此如果用户调整大小,图像将覆盖所有额外的 space。

这就是我向主画面添加背景图片的方式 window:

bgImage=PhotoImage(file="./Colors/bgImage.png")
bgLable = Label(root,image = bgImage)
bgLable.image = bgImage
bgLable.grid(row=0,column=0,columnspan=3,rowspan=8)

然而,当我尝试对框架做同样的事情时,它也会调整框架的大小:

FrameTL = Frame(root, width = 100)
FrameTL.grid(row = 0, column = 0, rowspan = 20, columnspan = 1, sticky = W+N+S)
TLbg = Label(FrameTL,image = bgImage)
TLbg.image = bgImage
TLbg.grid(row=0,column=0)

我尝试了其他一些方法,但它们会扭曲图像以适应 window/frame,而我不想扭曲图像。

编辑:

我找到了某种变通方法。我没有使用框架,而是将 .grid 用于我所有的小部件,除了我需要在侧边菜单上的小部件。侧面菜单小部件并没有疏远 window 的左上角,所以我试图使用框架来解决问题(现在使用 .place)。这些框架可以工作,但我找不到使框架透明的方法,所以我可以让我的背景远离我的根 window。这就是为什么我坚持尝试将背景图像添加到我的框架,但没有调整框架大小以适合图像。

Is there a way to place the image into the background of the frame without the frame changing in size?

是的。使用 place 和相对坐标。以下将把带有您的图像的标签放在根 window 的中心,并且不会影响任何其他小部件。

some_frame = tk.Frame(root, borderwidth=2, relief="raised", width=200, height=200)
...
background = tk.Label(some_frame, text="I am in the center", background="pink")
background.place(relx=.5, rely=.5, anchor="c")

这是我认为 place 优于使用 gridpack 的少数情况之一。官方文档是这样说的 place:

Unlike many other geometry managers (such as the packer) the placer does not make any attempt to manipulate the geometry of the master windows or the parents of slave windows (i.e. it does not set their requested sizes). To control the sizes of these windows, make them windows like frames and canvases that provide configuration options for this purpose.