在Tkinter中设置背景(python)

问题描述 投票:0回答:1

我想知道是否有可能在Tkinter的框架上设置背景图像。我尝试通过在带有图像的框架内设置画布来进行尝试,但是未成功导入。

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)
python canvas tkinter background frame
1个回答
0
投票

这里是在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()
© www.soinside.com 2019 - 2024. All rights reserved.