设置图像为Tkinter窗口背景?

huangapple go评论78阅读模式
英文:

Set image as window background Tkinter?

问题

I understand how to get an image into tkinter with ImageTk.PhotoImage(Image.open("Image")), but is it possible to set that image as the background of a window? I know that you can set it as a label then pack it into the window, but can you actually just set it as the background for the window?

我明白如何使用 ImageTk.PhotoImage(Image.open("Image")) 将图像加载到 tkinter 中,但是否可能将该图像设置为窗口的背景?我知道你可以将它设置为标签然后将其打包到窗口中,但是否可以直接将其设置为窗口的背景?

I tried to use the bg argument of root.config(), but it just came up with the error _tkinter.TclError: unknown color name "pyimage1", from which I deduced that that was not the way to go about it.

我尝试使用 root.config()bg 参数,但它出现了错误 _tkinter.TclError: unknown color name "pyimage1",因此我推断这不是解决方法。

英文:

I understand how to get an image into tkinter with ImageTk.PhotoImage(Image.open("Image")), but is it possible to set that image as the background of a window? I know that you can set it as a label then pack it into the window, but can you actually just set it as the background for the window?

I tried to use the bg argument of root.config(), but it just came up with the error _tkinter.TclError: unknown color name "pyimage1", from which I deduced that that was not the way to go about it.

答案1

得分: 0

抱歉,我只会回应您要求的部分翻译,这是原文的翻译:

"不,根窗口和Toplevel的实例不支持背景图像。创建一个带有图像的标签,并配置它以覆盖整个窗口是很简单的。然后,您可以将所有其他小部件放在该标签内。"

英文:

No, the root window and instances of Toplevel do not support a background image. It’s trivial to create a label with an image, and configure it to cover the entire window. You can then put all other widgets inside the label.

huangapple
  • 本文由 发表于 2023年5月13日 07:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定