0

Im attempting to insert an image on a canvas in my tkinter GUI. I'm able to insert an image usually but when doing it on a canvas I get the errors: "pyimage does not exist" or "file directory not found"

In the class "PageMain" I have created a widget and followed the answer on this thread - How to insert an image in a canvas item?

However it still says the file cannot be found despite using the answer.

from Tkinter import *
import ttk 
import sqlite3
import Tkinter as tk
import Tkinter
import sys
import StringIO
from PIL import Image, ImageTk
import os



class MyApp(Tk):
    def __init__(self):
        Tk.__init__(self)
        container = ttk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        for F in (PageMain, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='NSEW')
        self.show_frame(PageMain)


    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()



class PageMain(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.make_widget()

    def make_widget(self):
        canvas = Canvas(self, width="1000", height="600")

        img = PhotoImage(file='D:\plogo\logo.png')
        canvas.create_image(50, 10, image=img, anchor=NW)


         # demo button to change page
        btnChange = Button(canvas, text="Change", font="Arial 16",
                           command=lambda: self.controller.show_frame(PageOne),
                           bg="#a0ccda")
        btnChange .place(x=600, y=550, width="100", height="50")

        canvas.pack()



        def change_page(self):
            pass




if __name__ == '__main__':
    app = MyApp()
    app.title('Music Match')
    app.mainloop()
Saad
  • 3,340
  • 2
  • 10
  • 32
T.Gilmour
  • 43
  • 2
  • 12

1 Answers1

0

you must keep a reference to the image, thanks Bryan, see below, especially to

w = tk.Canvas(self,)

w.img = tk.PhotoImage(file='logo.png')

#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


class Main(ttk.Frame):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent
        self.init_ui()
      
    def init_ui(self):

        w = tk.Canvas(self, )
        w.img = tk.PhotoImage(file='logo.png')
        w.create_image(50,50, anchor=tk.N+tk.W, image=w.img)
        w.pack(expand=1)
  
    def on_close(self):
        self.parent.on_exit()

class App(tk.Tk):
    """Start here"""

    def __init__(self):
        super().__init__()

        self.protocol("WM_DELETE_WINDOW", self.on_exit)
            
        self.set_style()
        self.set_title()
       
        frame = Main(self,)
        frame.pack(fill=tk.BOTH, expand=1)

    def set_style(self):
        self.style = ttk.Style()
        #('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
        self.style.theme_use("clam")
        
    def set_title(self):
        s = "{0}".format('Simple App')
        self.title(s)
        
    def on_exit(self):
        """Close all"""
        if messagebox.askokcancel(self.title(), "Do you want to quit?", parent=self):
            self.destroy()               
    
if __name__ == '__main__':
    app = App()
    app.mainloop()

enter image description here

Community
  • 1
  • 1
1966bc
  • 1,148
  • 1
  • 6
  • 11