我想知道是否可以在Tkinter的框架上设置背景图像。我尝试通过在Frame
中设置一个带有图像的画布来进行尝试,但未成功导入。
def background(self):
my_background = Canvas(self.__Frame4, bg="black")
filename = PhotoImage(file=r"images\main_bg.png")
background_label = Label(self.__Frame4, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
my_background.pack(expand=1, fill=BOTH)
这里是在Label中导入图像的示例:
import tkinter as tk
from PIL import ImageTk, Image
def create_img(frame, path_to_img):
width = 100
height = 140
img = Image.open(path_to_img)
img = img.resize((width,height), Image.ANTIALIAS)
photoImg = ImageTk.PhotoImage(img)
img_label = tk.Label(frame, image=photoImg, width=width)
img_label.image = photoImg
return img_label
root = tk.Tk()
root.grid_rowconfigure(0, weight=1, minsize=1)
root.grid_columnconfigure(0, weight=1, minsize=1)
my_image = create_img(root, "images\main_bg.png")
my_image.grid(row=1, column=0)
root.mainloop()