python - Tkinter images crashing -
so made script in python tkinter , thing first tkinter window pops without problems when code goes second window says :
_tkinter.tclerror: image "pyimage1" doesn't exist
and didn't find helped me, me please ? here code :
from tkinter import * pil import imagetk, image def choose(): global name, chosen name = name1.get() chosen = chosen1.get() print name print chosen root0.quit() root0 = tk() name1 = entry(root0) name1.pack() chosen1 = entry(root0) chosen1.pack() button(root0, text="enter", command=choose).pack() root0.mainloop() root = tk() img = imagetk.photoimage(image.open('person1.png')) panel1 = label(root, image = img) panel1.pack(side="left") img2 = imagetk.photoimage(image.open('person2.png')) panel2 = label(root, image = img2) panel2.pack(side="right") root.mainloop()
by way, python version 2.7
this side effect of using 2 roots (tk()
instances). images default associate first root window. quick fix provide image correct root:
img2 = imagetk.photoimage(image.open('person2.png'), master=root)
the proper fix never use more 1 tk()
. put code frame instances, , destroy 1 , load other when time right:
import tkinter tk def choose(): global name, chosen name = name1.get() chosen = chosen1.get() print name print chosen frame0.destroy() # kill frame frame1.pack() # open new frame root = tk.tk() frame0 = tk.frame(root) name1 = tk.entry(frame0) name1.pack() chosen1 = tk.entry(frame0) chosen1.pack() tk.button(frame0, text="enter", command=choose).pack() frame1 = tk.frame(root) img = imagetk.photoimage(image.open('person1.png')) panel1 = tk.label(frame1, image = img) panel1.pack(side="left") img2 = imagetk.photoimage(image.open('person2.png')) panel2 = tk.label(frame1, image = img2) panel2.pack(side="right") #start program frame0.pack() # load frame0 root.mainloop()
note moved away evil wildcard imports (from module import *
).
Comments
Post a Comment