从另一个python文件调用函数后,canvas不显示图像
我有两个文件: mainfile.py:单击have按钮时,它应该调用另一个文件(showfile.py),该文件具有包含画布图像的函数,但未加载任何内容 mainfile.py从另一个python文件调用函数后,canvas不显示图像,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我有两个文件: mainfile.py:单击have按钮时,它应该调用另一个文件(showfile.py),该文件具有包含画布图像的函数,但未加载任何内容 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__(se
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)
但当我运行此文件时,,,它会在没有加载图像的情况下弹出,并给出错误:tkinter.TclError:image“pyimage1”不存在这是一个错误,当您使用多个
Tk()
,将子窗口替换为我替换的Toplevel()。但还是同样的问题同样的错误?或者呢?如果函数中加载的图像的引用没有保存在全局空间中,那么它将被垃圾收集。是的,它会给出相同的错误