Python 如何使用tkinter添加背景图像

Python 如何使用tkinter添加背景图像,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在尝试向GUI添加背景图像。我正在使用tkraise()浏览页面。我使用for循环初始化“page”类型的对象,并使用show\u frame方法在单击相应按钮时将每个页面提升到顶部 from tkinter import * class App(Tk): def __init__(self, *args, **kwargs): Tk.__init__(self, *args, **kwargs) #Setup Menu MainM

我正在尝试向GUI添加背景图像。我正在使用
tkraise()
浏览页面。我使用for循环初始化“page”类型的对象,并使用
show\u frame
方法在单击相应按钮时将每个页面提升到顶部

from tkinter import *


class App(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        #Setup Menu
        MainMenu(self)
        #Setup Frame
        canvas = Canvas(self, height = 700, width = 800)
        canvas.pack()

        container = Frame(self, bg = '#80c1ff', bd = 5)
        container.place(relx = 0.5, rely = 0.05, relwidth = 0.90, relheight = 0.90, anchor = 'n')
        container.grid_rowconfigure(1, weight=1)
        container.grid_columnconfigure(0, weight=1)
        # container = Frame(self, bg = '#80c1ff', bd=5, height = 600, width = 600)
        # container.place(relx = 0.5, rely = 0.05, relwidth = 0.90, relheight = 0.90, anchor = 'n')


        self.frames = {}

        for F in (StartPage, addItem, deleteItem, viewItem):

            frame = F(container, self)

            self.frames[F] = frame
            frame.grid(row=1, column=0, sticky="nsew")

        self.show_frame(StartPage)  
    def show_frame(self, context):
        frame = self.frames[context]
        frame.tkraise()

class StartPage(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        

        label = Label(self, text="Start Page", font = 40)
        label.place(relx = 0.38, relheight = .08, rely = .10, relwidth = 0.25)

        view_item = Button(self, text = "View Items", font = 40, command = lambda:controller.show_frame(viewItem))
        view_item.place(relx  = 0.38, relheight = .08, rely = .50 , relwidth = 0.25)

        exit_window = Button(self, text = "Exit", font = 40, command = quit)
        exit_window.place(relx  = 0.38, relheight = .08, rely = .60 , relwidth = 0.25)

        add_item = Button(self, text = "Add Item", font = 40, command=lambda:controller.show_frame(addItem))
        add_item.place(relx  = 0.38, relheight = .08, rely = .30 , relwidth = 0.25)

        delete_item = Button(self, font = 40, text="Delete Item", command=lambda:controller.show_frame(deleteItem))
        delete_item.place(relx  = 0.38, relheight = .08, rely = .40 , relwidth = 0.25)


class addItem(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        start_page = Button(self, text="Start Page", command=lambda:controller.show_frame(StartPage))
        start_page.pack()


class deleteItem(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)


        start_page = Button(self, text="Start Page", command=lambda:controller.show_frame(StartPage))
        start_page.pack()


class viewItem(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)


        start_page = Button(self, text="Start Page", command=lambda:controller.show_frame(StartPage))
        start_page.pack()



class MainMenu:
    def __init__(self, master):
        menubar = Menu(master)
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="Exit", command=master.quit)
        menubar.add_cascade(label="File", menu=filemenu)
        master.config(menu=menubar)


app = App()

app.mainloop()

我尝试使用
PhotoImage
加载gif,并在标签对象上使用
place
,但没有显示任何内容。我在构造函数中使用了
PhotoImage
下面的
canvas.pack()

创建一个用于呈现墙纸的类,在每个页面上调用该类,并为每个类传入父小部件
self

class BackGround:
    def __init__(self,root):
        self.canvas = Canvas(root)
        self.canvas.place(relx=0.5, rely=0.5, relwidth=1, relheight=1, anchor='center')
        self.image_path = Image.open("Path to image")
        self.image = ImageTk.PhotoImage(self.image_path)
        self.canvas.image = self.image  # Keeping Reference to the Image
        self.canvas.create_image(50, 10, image=self.image, anchor=NW)

因此,例如,如果您希望
类附加项(框架):
有一张壁纸,请执行
背景(自身)

这就是它的样子:

class addItem(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        BackGround(self)

        start_page = Button(self, text="Start Page", command=lambda: controller.show_frame(StartPage))
        start_page.pack()

同样的原则也适用于其他页面。

显示您尝试过的代码(使用
PhotoImage()
Label
)。如果您希望画布“显示”其上分层的框架,这根本不起作用-所有Tkinter小部件本质上都是不透明的。请注意!非常感谢。