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

原代码如下:


    
    
  1. from tkinter import *
  2. from tkinter.filedialog import askopenfilename
  3. from PIL import Image,ImageTk
  4. class mainapp:
  5. def __init__(self):
  6. root=Tk()
  7. root.title( “Several”)
  8. root.geometry( ‘200x300’)
  9. Button(root,text= ‘自动取点’,command=self.another1).place(relx= 0.3,rely= 0.15,anchor=CENTER)
  10. Button(root,text= ‘图片翻转’).place(relx= 0.7,rely= 0.15,anchor=CENTER)
  11. root.mainloop()
  12. def another1(self):
  13. qudian()
  14. def another2(self):
  15. fanzhuan
  16. class qudian:
  17. def __init__(self):
  18. r1=Tk()
  19. r1.title( ‘自动取点’)
  20. r1.geometry( ‘400x400’)
  21. Label(r1,text= ‘图片路径:’).place(relx= 0,rely= 0.04)
  22. e1=Entry(r1)
  23. e1.place(relx= 0.15,rely= 0.04)
  24. Button(r1,text= ‘选择图片’).place(relx= 0.52,rely= 0.02)
  25. Label(r1,text= “所选择的图片:”).place(relx= 0,rely= 0.13)
  26. img_open=Image.open( ‘desert.jpg’)
  27. img_open.thumbnail(( 200, 200))
  28. img=ImageTk.PhotoImage(img_open)
  29. l1=Label(r1,image=img)
  30. l1.place(relx= 0,rely= 0.18)
  31. r1.mainloop()
  32. class fanzhuan:
  33. def __init__(self):
  34. r2=Tk()
  35. r2.mainloop()
  36. mainapp()

运行以后报错:



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

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

修改后的代码:


    
    
  1. class qudian:
  2. def __init__(self):
  3. r1=Toplevel()
  4. r1.title( '自动取点')
  5. r1.geometry( '400x400')
  6. Label(r1,text= '图片路径:').place(relx= 0,rely= 0.04)
  7. e1=Entry(r1)
  8. e1.place(relx= 0.15,rely= 0.04)
  9. Button(r1,text= '选择图片').place(relx= 0.52,rely= 0.02)
  10. Label(r1,text= "所选择的图片:").place(relx= 0,rely= 0.13)
  11. img_open=Image.open( 'desert.jpg')
  12. img_open.thumbnail(( 200, 200))
  13. img=ImageTk.PhotoImage(img_open)
  14. l1=Label(r1,image=img)
  15. l1.place(relx= 0,rely= 0.18)
  16. r1.mainloop()


参考:https://stackoverflow.com/questions/26097811/image-pyimage2-doesnt-exist

原代码如下:


  
  
  1. from tkinter import *
  2. from tkinter.filedialog import askopenfilename
  3. from PIL import Image,ImageTk
  4. class mainapp:
  5. def __init__(self):
  6. root=Tk()
  7. root.title( “Several”)
  8. root.geometry( ‘200x300’)
  9. Button(root,text= ‘自动取点’,command=self.another1).place(relx= 0.3,rely= 0.15,anchor=CENTER)
  10. Button(root,text= ‘图片翻转’).place(relx= 0.7,rely= 0.15,anchor=CENTER)
  11. root.mainloop()
  12. def another1(self):
  13. qudian()
  14. def another2(self):
  15. fanzhuan
  16. class qudian:
  17. def __init__(self):
  18. r1=Tk()
  19. r1.title( ‘自动取点’)
  20. r1.geometry( ‘400x400’)
  21. Label(r1,text= ‘图片路径:’).place(relx= 0,rely= 0.04)
  22. e1=Entry(r1)
  23. e1.place(relx= 0.15,rely= 0.04)
  24. Button(r1,text= ‘选择图片’).place(relx= 0.52,rely= 0.02)
  25. Label(r1,text= “所选择的图片:”).place(relx= 0,rely= 0.13)
  26. img_open=Image.open( ‘desert.jpg’)
  27. img_open.thumbnail(( 200, 200))
  28. img=ImageTk.PhotoImage(img_open)
  29. l1=Label(r1,image=img)
  30. l1.place(relx= 0,rely= 0.18)
  31. r1.mainloop()
  32. class fanzhuan:
  33. def __init__(self):
  34. r2=Tk()
  35. r2.mainloop()
  36. mainapp()

运行以后报错:

扫描二维码关注公众号,回复: 3011750 查看本文章



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

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

修改后的代码:


  
  
  1. class qudian:
  2. def __init__(self):
  3. r1=Toplevel()
  4. r1.title( '自动取点')
  5. r1.geometry( '400x400')
  6. Label(r1,text= '图片路径:').place(relx= 0,rely= 0.04)
  7. e1=Entry(r1)
  8. e1.place(relx= 0.15,rely= 0.04)
  9. Button(r1,text= '选择图片').place(relx= 0.52,rely= 0.02)
  10. Label(r1,text= "所选择的图片:").place(relx= 0,rely= 0.13)
  11. img_open=Image.open( 'desert.jpg')
  12. img_open.thumbnail(( 200, 200))
  13. img=ImageTk.PhotoImage(img_open)
  14. l1=Label(r1,image=img)
  15. l1.place(relx= 0,rely= 0.18)
  16. r1.mainloop()


参考:https://stackoverflow.com/questions/26097811/image-pyimage2-doesnt-exist

猜你喜欢

转载自blog.csdn.net/polyhedronx/article/details/81911620
今日推荐