繁体   English   中英

如何使用 tkinter 添加背景图片

[英]How to add a background image using tkinter

我正在尝试向我的 GUI 添加背景图像。 我正在使用tkraise()来浏览页面。 我正在使用 for 循环来初始化“页面”类型的对象,并使用show_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 并在 label object 上使用place但它没有显示任何内容。 我在构造函数中使用PhotoImage canvas.pack()下面的 PhotoImage。

创建一个 class 用于渲染壁纸,在每个页面调用 class 并为每个 class 传入父 widget 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):有一个壁纸做BackGround(self)

这是它的样子:

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()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM