0

我正在尝试向上面有图片的框架添加一个按钮。只要我只有一帧,我就可以很容易地做到这一点。但是当我有多个帧并尝试使用按钮加载不同的帧时,图中的按钮不起作用或显示其图片。该按钮是可见的,但其背景只是白色。它没有显示它的图片。这是我的代码

from tkinter import *
import tkinter as tk


LARGE_FONT= ("Verdana", 12)


class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        
        tk.Tk.__init__(self, *args, **kwargs)
        # Create Windows, Frames
        container = tk.Frame(self)
        
        container.pack(side="top", fill="both", expand = True)
        # frame size
        container.grid_rowconfigure(0, minsize = 500)
        container.grid_columnconfigure(0, minsize = 800)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

        
class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
       
        
        #images of the buttons
        home_btn = PhotoImage(file='/home/pi/Documents/Bobby/Pictures/Home_Off.png')
       
        label = tk.Label(self, text="Hello There", font=LARGE_FONT)
        self.configure(bg='black') # background color
        label.pack(pady=10,padx=10)
       
        button = tk.Button(self, text="Visit Page 1", 
                           command=lambda: controller.show_frame(PageOne))
        button.pack()

        button2 = tk.Button(self, text="Visit Page 2",
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()
                            
        home = tk.Button(self, image=home_btn,
                           command=lambda: controller.show_frame(PageOne))
        home.pack() 
 

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = tk.Button(self, text="Page Two",
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = tk.Button(self, text="Page One",
                            command=lambda: controller.show_frame(PageOne))
        button2.pack()
        

app = SeaofBTCapp()
app.mainloop()

buttonbutton1并且button2工作正常。但是我称之为“主页”的按钮,它有自己的背景图片不起作用。图片未显示。它只是一个白色背景。当我单击它时,它什么也不做。“主页”按钮位于class StartPage(tk.Frame)

任何帮助,将不胜感激。

谢谢

4

0 回答 0