【问题标题】:Set background in Tkinter [duplicate]在 Tkinter 中设置背景 [重复]
【发布时间】:2020-01-19 15:33:31
【问题描述】:
我想知道是否可以在 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)
【问题讨论】:
标签:
python
canvas
tkinter
background
frame
【解决方案1】:
这是一个在标签中导入图像的示例:
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()