添加5行代码后,图像“ pyimage5”不存在错误

时间:2019-06-25 12:12:27

标签: python python-3.x python-2.7 tkinter tk

我正试图从另一个类的输入框中调用String到公共Var。用于调用Var接缝的代码可以正常工作,但在第二类错误中出现了错误'''image“ pyimage5”不存在''',因此我添加了新的代码行。以前一切都很好玩。

我添加的代码行:

class StartPage(tk.Frame):

     def __init__(self, parent, controller):
         tk.Frame.__init__(self,parent)

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent,bg=Gray_Back_Page_1)

class StartPage(tk.Frame):

     def __init__(self, parent, controller):
         super(StartPage,self).__init__()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        super(PageOne,self).__init__(parent,bg=Gray_Back_Page_1)

并在类外添加以下代码

StartPage_object = StartPage(tk.Frame, SeaofBTCapp) 
USER = StartPage_object.Username_Text.get() 
PASSWORD = StartPage_object.Password_Text.get()

图像代码Var = Image.open(Image_File +"\File_Name.png")似乎可以在我的初始页中工作,但在我的第一页中却出现错误

请参见下面的完整代码:

desktop = os.path.expanduser("~\Desktop")
Image_File = os.path.expanduser("~\Desktop\file")

#===============Frame==========================Container====================>
class SeaofBTCapp(tk.Tk,object):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Name")
        tk.Tk.geometry(self,"1360x728")
        tk.Tk.iconbitmap(self, default= desktop + "\icon.ico")
        self.resizable(False, False)
        #tk.Tk.overrideredirect(False)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=10)
        container.grid_columnconfigure(0, weight=10)

        self.frames = {}

        for F in (StartPage, PageOne):

            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()

#===============Start=========================Page====================>
class StartPage(tk.Frame):

     def __init__(self, parent, controller):
         super(StartPage,self).__init__()

     #=============Set_Background==================>
     load1 = Image.open(Image_File +"\Login_Bcakground.png")
     render1 =ImageTk.PhotoImage(load1)

     #========= Reset_username & Password ===========>
     self.Username_Text= tk.StringVar()
     self.Password_Text= tk.StringVar()

     #==== Creating Buttons, Entry Box and Labels with there commands =====>
     User_Name_Entry = tk.Entry(self,textvariable = self.Username_Text, fg = 
                        Entry_Box_Text_Colour,bg = 
                        Entry_Box_Back_White,borderwidth = 0, 
                        font=Normal_Text,width = 30)
     User_Name_Entry.place(x=795,y=282)

     User_Pass_Entry = tk.Entry(self,textvariable = selfPassword_Text, fg = 
                       Entry_Box_Text_Colour,bg = 
                       Entry_Box_Back_White,borderwidth = 0, 
                       font=Normal_Text,width = 30)
     User_Pass_Entry.place(x=795,y=329)

#===============Login==========================Var====================>

StartPage_object = StartPage(tk.Frame, SeaofBTCapp) 
USER = StartPage_object.Username_Text.get() 
PASSWORD = StartPage_object.Password_Text.get()

#===============Page==========================One====================>
class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        super(PageOne,self).__init__(parent,bg=Gray_Back_Page_1)

        photo = ImageTk.PhotoImage(Image.open(Image_File +"\Splach_Page.png"))      
        vlabel=tk.Label(self,text = "",image=photo)
        vlabel.image = photo
        vlabel.place (x=-1,y=-5,relwidth=1, relheight=1)

错误行:

image "pyimage5" doesn't exist
Stack trace:
 >  File "C:\Users\MainUser\source\v_1_1.py", line 251, in __init__
 >    vlabel=tk.Label(self,text = "",image=photo)
 >  File "C:\Users\MainUser\source\v_1_1.py", line 93, in __init__
 >    frame = F(container, self)
 >  File "C:\Users\MainUser\source\v_1_1.py", line 2736, in <module>
 >    app = SeaofBTCapp()
Loaded '__main__'

3 个答案:

答案 0 :(得分:1)

对我来说,问题在于 Spyder 而不是代码。 我改用 Jupyter Notebook,它按预期工作

答案 1 :(得分:0)

这看起来像是tkinter中的已知错误,其中python垃圾收集器删除了照片。为防止这种情况,您需要创建对该图像的全局引用。这样的事情应该起作用:

class PageOne(tk.Frame):
    images = []

    def __init__(self, parent, controller):
        super(PageOne,self).__init__(parent,bg=Gray_Back_Page_1)

        photo = ImageTk.PhotoImage(Image.open(Image_File +"\Splach_Page.png"))
        PageOne.images.append(photo)     
        vlabel=tk.Label(self,text = "",image=photo)
        vlabel.image = photo
        vlabel.place (x=-1,y=-5,relwidth=1, relheight=1)

希望对您有用!

答案 2 :(得分:0)

这没有用,并给我同样的错误,但是您的想法帮助我以另一种方式解决了它。我回到原始代码,并使用list方法解决了我的第一个问题。

#=============Use_List_For_Capturing_Var_In_Classes==================

Name_oF_User_Loged_In = []
Name_oF_User_Loged_In.append("User Name")

class StartPage(tk.Frame):

   global login_data_Pass
   global login_data_User
   global Name_oF_User_Loged_In

   def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
   #=============Set_Background==================>
   load1 = Image.open(Image_File +"\Login_Bcakground.png")
   render1 =ImageTk.PhotoImage(load1)

   def Login_Check():
                USER1 = self.Username_Text.get()
                PASSWORD1 = self.Password_Text.get()
                login_data_Pass.pop(0)
                login_data_User.pop(0)
                login_data_Pass.append(PASSWORD1)
                login_data_User.append(USER1) 

   #==== Creating Buttons, Entry Box and Labels with there commands =====>

   Login_button = tk.Button(self, text="Login", 
           fg="#d0cece",bg="#3f9a84",borderwidth = 0, font=Normal_Text,height 
           =1,width = 10,
           command=lambda: Login_Check())
   Login_button.place(x=1010,y=380)

   User_Name_Entry = tk.Entry(self,textvariable = self.Username_Text, fg = 
                    Entry_Box_Text_Colour,bg = 
                    Entry_Box_Back_White,borderwidth = 0, 
                    font=Normal_Text,width = 30)
   User_Name_Entry.place(x=795,y=282)

   User_Pass_Entry = tk.Entry(self,textvariable = selfPassword_Text, fg = 
                   Entry_Box_Text_Colour,bg = 
                   Entry_Box_Back_White,borderwidth = 0, 
                   font=Normal_Text,width = 30)
   User_Pass_Entry.place(x=795,y=329)


class PageOne(tk.Frame):

  global login_data_Pass
  global login_data_User
  global Name_oF_User_Loged_In

  def __init__(self, parent, controller):
      tk.Frame.__init__(self, parent,bg=Gray_Back_Page_1)

         photo = ImageTk.PhotoImage(Image.open(Image_File +"\Splach_Page.png"))      
         vlabel=tk.Label(self,text = "",image=photo)
         vlabel.image = photo
         vlabel.place (x=-1,y=-5,relwidth=1, relheight=1)

         user = login_data_User[0]
         password = login_data_Pass[0]