我正在尝试将小部件放在背景图像上方,但背景图像显示在小部件上方。
代码
import tkinter as tk
class baseclass(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
container = tk.Frame(self)
container.pack(side="top",fill="both",expand=True)
container.grid_rowconfigure(0,weight=1)
container.grid_columnconfigure(0,weight=1)
self.title("REMOTE STREETLIGHT MANAGEMENT")
self.frames = {}
for F in (startpage,firstPage):
page_name = F.__name__
frame = F(parent=container,controller=self)
self.frames[page_name] = frame
frame.grid(row=0,column=0,sticky="nsew")
self.show_frame("startpage")
def show_frame(self,page_name):
frame = self.frames[page_name]
frame.tkraise()
class startpage(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller = controller
c=tk.Canvas(self,bg="red", height=250,width=300)
img1_path=r"D:\StreetLight\sl1.gif"
img1=tk.PhotoImage(file=img1_path)
background_label = tk.Label(self, image=img1)
title_label=tk.Label(self,text='Welcome to the Remote Monitoring portal\n')
title_label.pack()
background_label.image=img1
background_label.pack()
#attempt to login button
attempt_login = tk.Button(self,text="Login",
command=lambda: controller.show_frame("firstPage"))
attempt_login.pack(pady=4)
c.pack()
class firstPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.controller=controller
title_label=tk.Label(self,text='Welcome to the Remote Monitoring portal\n')
title_label.pack()
select_area_label=tk.Label(self,text="SELECT AREA\n")
select_area_label.pack()
back = tk.Button(self,text="Sign Out",
command=lambda: controller.show_frame("startpage"))
back.pack(pady=4)
if __name__ == "__main__":
app = baseclass()
app.mainloop()
答案 0 :(得分:0)
根据this question's的答案,要在tkinter中使用图片作为背景,您应该使用place
而不是pack
来定义其位置。在您的代码中,尝试替换:
background_label.pack()
与
background_label.place(x=0, y=0, relwidth=1, relheight=1)