tkinter设置背景图片
在Tkinter中,可以使用Canvas或Label来设置背景图片。
使用Canvas:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
# 设置窗口大小和标题
root.geometry("500x500")
root.title("Background Image")
# 加载图片
bg_image = ImageTk.PhotoImage(Image.open("background.jpg"))
# 创建Canvas
canvas = Canvas(root, width=500, height=500)
# 显示图片
canvas.create_image(0, 0, image=bg_image, anchor="nw")
# 将Canvas放置在顶部
canvas.pack()
# 运行窗口
root.mainloop()
使用Label:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
# 设置窗口大小和标题
root.geometry("500x500")
root.title("Background Image")
# 加载图片
bg_image = ImageTk.PhotoImage(Image.open("background.jpg"))
# 创建Label
bg_label = Label(root, image=bg_image)
# 将Label放置在顶部
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
# 运行窗口
root.mainloop()
``
原文地址: https://www.cveoy.top/t/topic/eE4V 著作权归作者所有。请勿转载和采集!