我试图为程序的不同页面设置不同的背景,问题是只有 init() 方法中的窗口具有背景图像。
class windows():
def __init__(self):
self.screen=Tk()
self.font='Times New Roman'
#size and initial position of the window
self.screen.geometry("1100x700+200+50")
#colour of screen
self.canva= Canvas(self.screen, width= 1200, height= 800)
self.canva.pack()
self.img= ImageTk.PhotoImage(Image.open("bg_main.png"))
self.canva.create_image(0,0,anchor=NW,image=self.img)
#self.canva.create_text(550,100,text="\tWELCOME TO \nQUESTION PAPER GENERATOR",font=(self.font,30,'bold'),fill='black')
#fix window size
self.screen.maxsize(1100,700)
self.screen.minsize(1100,700)
#creating buttons
self.add_ques=Button(self.screen,text="ADD QUESTIONS",fg="#02203d",font=(self.font,15,'bold'),bg="#fba75b",activebackground="#6d7a6d",command=lambda:self.add())
self.add_ques.place(x=350,y=200,height=50,width=300)
self.generate=Button(self.screen,text="GENERATE PAPER",fg="#02203d",font=(self.font,15,'bold'),bg="#fba75b",activebackground="#6d7a6d",command=lambda:self.generator())
self.generate.place(x=350,y=310,height=50,width=300)
self.add_ques=Button(self.screen,text="CREATE DATABASE",fg="#02203d",font=(self.font,15,'bold'),bg="#fba75b",activebackground="#6d7a6d",command=lambda:self.gen_database())
self.add_ques.place(x=450,y=400,height=50,width=300)
self.add_ques=Button(self.screen,text="ADD SUBJECTS",fg="#02203d",font=(self.font,15,'bold'),bg="#fba75b",activebackground="#6d7a6d",command=lambda:self.add_sub())
self.add_ques.place(x=450,y=515,height=50,width=300)
def add(self):
self.screen.destroy()
screen=Tk()
#destuction function
def destruction():
screen.destroy()
self.__init__()
#size and initial position of the window
screen.geometry("1100x700+200+50")
#colour of screen
bg = PhotoImage(file="bg.png")
label_bgImage = Label(screen, image=bg)
label_bgImage.place(x=0, y=0)
#fix window size
screen.maxsize(1100,700)
screen.minsize(1100,700)
在此我希望 add() 方法拥有自己的窗口和自己的背景,运行时它会打开而不会出现错误,但不会加载图像。
如何为类的不同方法添加不同的背景图片
问题可以解决。在 add(0 函数上。您忘记将
self.
添加到 self.img
command=self.add
。
与其他按钮小部件相同。self.screen.withdraw()
。self.
添加到 screen.screen 并将 Tk()
重命名为
Toplevel()
。self.
到self.screen.geometry
。self.
添加到 self.img = ...
self.
小部件参数中添加 Label
Label(self.screen, image=bg)
self.
添加到 self.screen
app = windows()
mainloop()
片段:
from tkinter import *
from PIL import Image, ImageTk
class windows():
def __init__(self):
self.screen = Tk()
self.font='Times New Roman'
#size and initial position of the window
self.screen.geometry("1100x700+200+50")
#colour of screen
self.canva= Canvas(self.screen, width= 1200, height= 800)
self.img= ImageTk.PhotoImage(Image.open("p2.png"))
self.canva.create_image(0,0,anchor=NW,image=self.img)
self.canva.pack()
#self.canva.create_text(550,100,text="\tWELCOME TO \nQUESTION PAPER GENERATOR",font=(self.font,30,'bold'),fill='black')
#fix window size
#creating buttons
self.add_ques=Button(self.screen,text="ADD QUESTIONS",fg="#02203d",font=(self.font,15,'bold'),bg="#fba75b",activebackground="#6d7a6d",command=self.add)
self.add_ques.place(x=350,y=200,height=50,width=300)
self.generate=Button(self.screen,text="GENERATE PAPER",fg="#02203d",font=(self.font,15,'bold'),bg="#fba75b",activebackground="#6d7a6d",command=lambda:self.generator())
self.generate.place(x=350,y=310,height=50,width=300)
self.add_ques=Button(self.screen,text="CREATE DATABASE",fg="#02203d",font=(self.font,15,'bold'),bg="#fba75b",activebackground="#6d7a6d",command=lambda:self.gen_database())
self.add_ques.place(x=450,y=400,height=50,width=300)
self.add_ques=Button(self.screen,text="ADD SUBJECTS",fg="#02203d",font=(self.font,15,'bold'),bg="#fba75b",activebackground="#6d7a6d",command=lambda:self.add_sub())
self.add_ques.place(x=450,y=515,height=50,width=300)
def add(self):
#self.screen.destroy()
self.screen.withdraw()
self.top = Toplevel()
self.top.title('New Window')
#destuction function
def destruction():
screen.destroy()
self.__init__()
#size and initial position of the window
self.top.geometry("1100x700+200+50")
#colour of screen
self.img = PhotoImage(file="p1.png")
label_bgImage = Label(self.top, bg='red',image=self.img)
label_bgImage.place(x=120, y=120)
self.top.maxsize(1100,700)
self.top.minsize(1100,700)
app = windows()
mainloop()
截图: