Python tkinter图像错误

时间:2016-01-28 15:03:12

标签: python tkinter

我正在做一个学校作业计划,但我遇到了问题。问题是当我调用MainWindow类时,它出现了TclError:图像“pyimage1”不存在。当我将MainWindow类移动到另一个文件时,它可以打开。我尝试修复它但失败了。 有人能帮我吗?

from Tkinter import *
import tkMessageBox as tm
from PIL import Image, ImageTk

class Login:
    def __init__(self):
        self.login_window=Tk()
        self.login_window.title('PROJECTBOOK')

        self.usernameVar=StringVar()
        self.passwordVar=StringVar()

        frame1=Frame(self.login_window)
        frame1.pack()
        Label(frame1,text='USERNAME:').grid(row=1,sticky=W)
        self.entry1=Entry(frame1,textvariable=self.usernameVar,width=30)
        self.entry1.grid(row=1,column=1)

        frame2=Frame(self.login_window)
        frame2.pack()
        Label(frame2,text='PASSWORD:').grid(row=2,sticky=W)
        self.entry2=Entry(frame2,textvariable=self.passwordVar,width=30)
        self.entry2.grid(row=2,column=1)

        frame3=Frame(self.login_window)
        frame3.pack()
        self.Login_Btn=Button(frame3,text='LOGIN',fg='blue',command=self.login_btn).grid(row=1,column=1)
        self.Register_Btn=Button(frame3,text='REGISTER',fg='blue',command=self.register_btn).grid(row=1,column=2)

        self.login_window.mainloop()

    def login_btn(self):
        username=self.entry1.get()
        password=self.entry2.get()
        if not username:
            tm.showinfo('PROJECTBOOK','USERNAME MISSING!')
        elif not password:
            tm.showinfo('PROJECTBOOK','PASSWORD MISSING!')
        elif username=='admin' and password=='secret':
            MainWindow()

    def register_btn(self):
        self.login_window.destroy()
        Register()

class Register:
    def __init__(self):
        self.register_window=Tk()
        self.register_window.title('REGISTER')

        self.usernameVar=StringVar()
        self.passwordVar=StringVar()
        self.confirmpasswordVar=StringVar()

        frame1=Frame(self.register_window)
        frame1.pack()
        Label(frame1,text='USERNAME:').grid(row=1,sticky=W)
        self.entry1=Entry(frame1,textvariable=self.usernameVar,width=30)
        self.entry1.grid(row=1,column=1)

        frame2=Frame(self.register_window)
        frame2.pack()
        Label(frame2,text='PASSWORD:').grid(row=2,sticky=W)
        self.entry2=Entry(frame2,textvariable=self.passwordVar,width=30)
        self.entry2.grid(row=2,column=1)

        frame3=Frame(self.register_window)
        frame3.pack()
        Label(frame3,text='CONFIRM PASSWORD:').grid(row=3,sticky=W)
        self.entry3=Entry(frame3,textvariable=self.confirmpasswordVar,width=20)
        self.entry3.grid(row=3,column=1)

        frame4=Frame(self.register_window)
        frame4.pack()
        self.Submit_Btn=Button(frame4,text='SUBMIT',fg='blue').grid(row=1,column=1)
        self.Back_Btn=Button(frame4,text='BACK',fg='red',command=self.backtologin).grid(row=1,column=2)

        self.register_window.mainloop()

    def backtologin(self):
        self.register_window.destroy()
        Login()

class MainWindow:
    def __init__(self):
        main_window=Tk()
        main_window.title('PROJECTBOOK')

        frame=Frame(main_window)
        frame.pack(side=TOP,fill=X)
        welcome = Image.open('welcome.png')
        photo = ImageTk.PhotoImage(welcome)
        Welcome_Label=Label(frame,image=photo)
        Welcome_Label.image = photo 
        Welcome_Label.pack()
        Library_Label=Label(frame,text='LIBRARY',font=("Times New Roman",30)).pack()

        searchbar=Frame(main_window)
        searchbar.pack(side=RIGHT,fill=X)
        self.search_var=StringVar()
        self.search_var.trace("w", lambda name, index, mode: self.update_list())
        self.entry = Entry(searchbar, textvariable=self.search_var, width=13)
        self.lbox = Listbox(searchbar, width=45, height=15)
        self.entry.pack()
        self.lbox.pack()
        self.update_list()


        toolbar=Frame(main_window)
        toolbar.pack(side=LEFT,fill=X)
        self.bookstore_btn=Button(toolbar,text='BOOKSTORE',fg='blue',height=3,width=20).grid(row=1,column=1)
        self.secondhand_btn=Button(toolbar,text='SECONDHAND BOOK',fg='blue',height=3,width=20).grid(row=2,column=1)
        self.tradein_btn=Button(toolbar,text='TRADE IN',fg='blue',height=3,width=20).grid(row=3,column=1)
        self.promotion_btn=Button(toolbar,text='PROMOTION',fg='blue',height=3,width=20).grid(row=4,column=1)
        self.logout_btn=Button(toolbar,text='LOGOUT',fg='red',height=3,width=20).grid(row=5,column=1)

        main_window.mainloop()

    def update_list(self):
        search_term=self.search_var.get()

        lbox_list=['A','B','C','D',
        'E','F','G']

        self.lbox.delete(0,END)

        for item in lbox_list:
            if search_term.lower() in item.lower():
                self.lbox.insert(END, item)


Login()

异常堆栈跟踪:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "C:\Users\User\Desktop\Login_Dialog.py", line 40, in login_btn
    MainWindow()
  File "C:\Users\User\Desktop\Login_Dialog.py", line 93, in __init__
    Welcome_Label=Label(frame,image=photo)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2591, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2090, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
TclError: image "pyimage1" doesn't exist

0 个答案:

没有答案