如果不调整帧大小以适应图像,则无法设置帧背景图像

2024-09-30 04:40:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以设置主窗口的背景,我不需要担心窗口试图适应背景图像,或者图像将小部件推开。

然而,当我试图用一个框架做同样的事情时,它变成了一个巨大的混乱。。。

我已经尝试了一些事情,但一切都与调整图像本身有关,我不想扭曲图像。我希望图像在框架中,而不是调整框架大小以适应图像。

有没有一种方法可以在不改变帧大小的情况下将图像放入帧的背景中?

编辑:注意:我使用的图像足够大,可以填满屏幕,所以如果用户正在调整大小,图像将覆盖所有额外的空间。

这就是我如何在主窗口中添加背景图像:

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

但是,当我尝试对帧执行相同的操作时,它也会调整帧的大小:

^{pr2}$

我还尝试了其他一些方法,但它们会扭曲图像以适合窗口/框架,我不想扭曲图像。

编辑:

我找到了一份工作。我需要使用我的小部件,而不是我的小部件。侧菜单小部件不在窗口的左上角,所以我尝试使用框架来解决问题(现在使用.place)。框架可以工作,但我找不到一种方法使框架透明,这样我可以保持我的背景从我的根窗口。这就是为什么我一直试图在我的框架中添加背景图像,但是没有调整框架大小以适应图像。


Tags: 方法用户图像image框架编辑屏幕部件
1条回答
网友
1楼 · 发布于 2024-09-30 04:40:44

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

是的。将place与相对坐标一起使用。下面将把标签和您的图像放在根窗口的中心,并且不会影响任何其他小部件。在

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优于使用grid或{}的少数几次之一。这是官方文件所说的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.

相关问题 更多 >

    热门问题