如何在 tkinter python 中为类的不同方法添加不同的背景图像

问题描述 投票:0回答:1

我试图为程序的不同页面设置不同的背景,问题是只有 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() 方法拥有自己的窗口和自己的背景,运行时它会打开而不会出现错误,但不会加载图像。

python python-3.x oop tkinter python-imaging-library
1个回答
0
投票

如何为类的不同方法添加不同的背景图片

问题可以解决。在 add(0 函数上。您忘记将

self.
添加到
self.img

  • 第 25 行,不要在命令关键字中使用 lambda
    command=self.add
    。 与其他按钮小部件相同。
  • 在第 39 行,添加
    self.screen.withdraw()
  • 在第 40 行,将
    self.
    添加到 screen.screen 并将
    Tk()
    重命名为
    Toplevel()
  • 49号线,
    self.
    self.screen.geometry
  • 在第 52 行,将
    self.
    添加到
    self.img = ...
  • 在第 53 行,在
    self.
    小部件参数中添加
    Label
    Label(self.screen, image=bg)
  • 在第 56 行和第 57 行将
    self.
    添加到
    self.screen
  • 在线60,添加这个
    app = windows()
  • 在第 61 行,添加
    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()

截图:

© www.soinside.com 2019 - 2024. All rights reserved.