【python】关于_tkinter.TclError: image "pyimage1" doesn't exist 问题的解决办法

原代码如下:

from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import Image,ImageTk

class mainapp:
    def __init__(self):
        root=Tk()
        root.title("Several")
        root.geometry('200x300')
        Button(root,text='自动取点',command=self.another1).place(relx=0.3,rely=0.15,anchor=CENTER)
        Button(root,text='图片翻转').place(relx=0.7,rely=0.15,anchor=CENTER)
        root.mainloop()
    def another1(self):
        qudian()
    def another2(self):
        fanzhuan
        
class qudian:
    def __init__(self):
        r1=Tk()
        r1.title('自动取点')
        r1.geometry('400x400')
        Label(r1,text='图片路径:').place(relx=0,rely=0.04)
        e1=Entry(r1)
        e1.place(relx=0.15,rely=0.04)
        Button(r1,text='选择图片').place(relx=0.52,rely=0.02)
        Label(r1,text="所选择的图片:").place(relx=0,rely=0.13)
        img_open=Image.open('desert.jpg')
        img_open.thumbnail((200,200))
        img=ImageTk.PhotoImage(img_open)
        l1=Label(r1,image=img)
        l1.place(relx=0,rely=0.18)
        r1.mainloop()
class fanzhuan:
    def __init__(self):
        r2=Tk()
        r2.mainloop()
mainapp()

运行以后报错:



之后去网上查了解决办法,在 https://zhidao.baidu.com/question/1800925191188288187.html 这个网址下找到了正确答案。因为在一个程序中只能存在一个根窗口,也就是说只能存在一个Tk(),其他的窗口只能以顶层窗口(Toplevel())的形式存在。

于是将qudian类下的Tk()改成Toplevel()后,问题完全解决。

修改后的代码:

class qudian:
    def __init__(self):
        r1=Toplevel()
        r1.title('自动取点')
        r1.geometry('400x400')
        Label(r1,text='图片路径:').place(relx=0,rely=0.04)
        e1=Entry(r1)
        e1.place(relx=0.15,rely=0.04)
        Button(r1,text='选择图片').place(relx=0.52,rely=0.02)
        Label(r1,text="所选择的图片:").place(relx=0,rely=0.13)
        img_open=Image.open('desert.jpg')
        img_open.thumbnail((200,200))
        img=ImageTk.PhotoImage(img_open)
        l1=Label(r1,image=img)
        l1.place(relx=0,rely=0.18)
        r1.mainloop()


  • 23
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值