微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tkinter创建图像功能错误:pyimage1不存在

如何解决Tkinter创建图像功能错误:pyimage1不存在

我正在为扫雷程序编写代码,但我不断收到一条错误消息,指出“ pyimage1”不存在。我怀疑该错误源自“ def frame”和/或“ class control”。我已经看到其他人发布了类似的问题,但无济于事。图像本身block.png和flag.png没有损坏。以前的测试表明,它可以工作,但是当我集成了多个窗口(一个带有视觉效果,另一个带有控制按钮)时却开始失败

from tkinter import *
from tkinter import Canvas
from PIL import ImageTk,Image
from time import sleep


class ResizingCanvas(Canvas):
    def __init__(self,parent,**kwargs):
        Canvas.__init__(self,**kwargs)
        self.bind("<Configure>",self.on_resize)
        self.height = self.winfo_reqheight()
        self.width = self.winfo_reqwidth()

    def on_resize(self,event):
        # determine the ratio of old width/height to new width/height
        wscale = float(event.width)/self.width
        hscale = float(event.height)/self.height
        self.width = event.width
        self.height = event.height
        # resize the canvas
        self.config(width=self.width,height=self.height)
        # rescale all the objects tagged with the "all" tag
        self.scale("all",wscale,hscale)


class minesweeper(Tk):

    def __init__(self,master):
        Tk.__init__(self)
        fr = Frame(self)
        fr.pack(fill=BOTH,expand=YES)
        self.canvas = ResizingCanvas(fr,width=940,height=920,bg="black",highlightthickness=0)
        self.count = 0
        self.start = 0
        self.newWindow = Toplevel(self.master)
        self.app = Control(self,self.newWindow)
        self.title("minesweeper")
        self.canvas.pack(fill=BOTH,expand=YES)
        x1 = 20
        y1 = 20
        x2 = 80
        y2 = 80
        self.block_pic = PhotoImage(file='C:/Users/akiva/OneDrive/Desktop/akiva/Python Files/block.PNG')
        self.flag_pic = PhotoImage(file='C:/Users/akiva/OneDrive/Desktop/akiva/Python Files/flag.PNG')
        for k in range(14):
            for i in range(15):
                self.canvas.create_rectangle(x1,y1,x2,y2,fill='white')
                x1 += 60
                x2 += 60
            x1 = 20
            x2 = 80
            y1 += 60
            y2 += 60

    def shift_image(self):
        if self.count == 0:
            Tk.canvas.itemconfig(self.block_pic,image=self.flag_pic)

    def end(self):
        self.start = 0
        del self.block_pic
        print("Game has ended")
        self.after(2000,quit())

    def frame(self):
        self.start += 1
        if self.start == 1:
            x1 = 50
            y1 = 50
            for i in range(14):
                for k in range(15):
                    self.canvas.create_image(x1,image=self.block_pic)
                    x1 += 60
                x1 = 50
                y1 += 60
            self.canvas.pack()
        else:
            print("Game has already started")


class Control(Toplevel):
    def __init__(self,master):
        self.master = master
        self.frame = Frame(self.master)
        start_button = Button(self.frame,text="Start Game",command=parent.frame,)
        stop_button = Button(self.frame,text="End Game",command=parent.end)
        start_button.pack()
        stop_button.pack()
        self.quitButton = Button(self.frame,text='Quit',width=25,command=self.close_windows)
        self.quitButton.pack()
        self.frame.pack()

    def close_windows(self):
        self.master.destroy()


{{1}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。