问题描述
我正在尝试向带有图片的框架添加一个按钮。只要我只有一帧,我就可以很容易地做到这一点。但是当我有多个框架并尝试使用按钮加载不同的框架时,如图所示的按钮不起作用或显示其图片。该按钮是可见的,但其背景只是白色。它没有显示它的图片。这是我的代码
from tkinter import *
import tkinter as tk
LARGE_FONT= ("Verdana",12)
class SeaofBTCapp(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,**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)
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,text="Page Two!!!",text="Page One",command=lambda: controller.show_frame(PageOne))
button2.pack()
app = SeaofBTCapp()
app.mainloop()
button
、button1
和 button2
工作正常。但是我称之为“主页”的具有自己背景图片的按钮不起作用。图片未显示。它只是一个白色的背景。当我点击它时它不会做任何事情。 “主页”按钮位于 class StartPage(tk.Frame)
:
任何帮助将不胜感激。
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)