如何在 Tkinter 中将图像用作背景?


如果我们创建一个 Tkinter 框架实例并在运行时显示窗口,那么它将显示默认输出画布。但是,我们可以使用 PhotoImage 方法和 Canvas 方法在 Tkinter 框架中将图像添加为背景。

由于 Tkinter 中的图像支持仅限于 Gif、PNG 和 PPM,因此 PhotoImage(GIF, PNG, PPM) 函数获取图像文件的位置,并显示具有图像背景的画布。

首先,我们将使用 PhotoImage 函数创建一个 PhotoImage 对象。

示例

from tkinter import *
from PIL import ImageTk

win = Tk()
win.geometry("700x300")

#Define the PhotoImage Constructor by passing the image file
img= PhotoImage(file='down.png', master= win)
img_label= Label(win,image=img)

#define the position of the image
img_label.place(x=0, y=0)

win.mainloop()

输出

运行上述代码片段将显示一个具有背景图像的窗口。

更新于: 2021 年 3 月 6 日

8K+ 浏览量

开启您的事业

完成课程并获得认证

开始学习
广告
© . All rights reserved.