canvas not display image after I call the function from another python file

  python, python-3.x, tkinter

I have two files:

mainfile.py : have button when I click on it should call the another file(showfile.py) which have function that contain canvas image , but nothing loaded ..

mainfile.py

from tkinter import *
import tkinter as tk
from showfile import sho

class Window(tk.Frame):


    def __init__(self, master=None):
        tk.Frame.__init__(self, master, bg='#333333')

        menu = tk.Menu(self.master)
        master.config(menu=menu)

        root.minsize(width=1080, height=750)
        width, height = root.winfo_screenwidth(), root.winfo_screenheight()
        root.geometry('%dx%d+0+0' % (width, height))
        root.iconbitmap("p.ico")

        full = Button(root, text="Full Screen", command=sho, padx=10, pady=10, borderwidth=6)
        full.pack(pady=20)
        full.place(x=100, y=500)



root = Tk()
root.geometry("%dx%d" % (300, 300))
root.title("BMP Image GUI")
app = Window(root)

app.pack(fill=tk.BOTH, expand=1)

root.mainloop()

showfile.py

from tkinter import *
from PIL import ImageTk, Image
import tkinter as tko



def sh():
    lan = tko.Tk()  # Creating instance of Tk class
    lan.title("Centering windows")
    lan.resizable(False, False)  # This code helps to disable windows from resizing
    window_height = 300
    window_width = 500
    screen_width = lan.winfo_screenwidth()
    screen_height = lan.winfo_screenheight()
    x_cordinate = int((screen_width / 2) - (window_width / 2))
    y_cordinate = int((screen_height / 2) - (window_height / 2))
    lan.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))

    filename = "iy.jpg"
    load = Image.open(filename)
    w, h = load.size
    image = filename
    render2 = ImageTk.PhotoImage(load)  # must keep a reference to this

    lastwidth, lasthight = w,h
    image = load.resize((int(lastwidth), int(lasthight)), Image.ANTIALIAS)
    canvas2 = tko.Canvas(lan,width=lastwidth, height=lasthight)
    canvas2.pack(fill=tko.BOTH, expand=True)
    canvas2.pack()
    canvas2.place(relx=0.5, rely=0.5, anchor=CENTER)

    render2 = ImageTk.PhotoImage(image)

    canvas2.create_image(lastwidth / 2, lasthight / 2, image=render2, anchor=CENTER)

    render2 = ImageTk.PhotoImage(load)

but when I run this files ,,, it popup without loaded image ,, and give error : _tkinter.TclError: image "pyimage1" doesn’t exist

Source: Python Questions

LEAVE A COMMENT