Too early to create image at NewWindow












1















I want to display the image in a new window, but I get an error.



This is my error code



photo = PhotoImage(file='img/dog')
File "C:UsersHyojaeAnaconda3libtkinter__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:UsersHyojaeAnaconda3libtkinter__init__.py", line 3486, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image


And this is my code sample.



I would appreciate your help.



from tkinter import *

def messageWindow():
win = Tk()
win.geometry('300x200')

root.destroy()


photo2 = PhotoImage(file="img/dog1.gif")
label1 = Label(win, image=photo2)
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)

win.mainloop()

root = Tk()
photo = PhotoImage(file="img/dog2.gif")
label1 = Label(root, image=photo)
label1.pack()


Button(root, text='Bring up Message', command=messageWindow).pack()


root.mainloop()









share|improve this question























  • Try moving the line root.destroy() before win = Tk() to avoid multiple instances of Tk() which may cause problems.

    – acw1668
    Nov 22 '18 at 8:28











  • Or you can specify the Tk instance to be used when initiating the PhotoImage instance: photo2 = PhotoImage(file="img/dog1.gif", master=win).

    – acw1668
    Nov 22 '18 at 8:52


















1















I want to display the image in a new window, but I get an error.



This is my error code



photo = PhotoImage(file='img/dog')
File "C:UsersHyojaeAnaconda3libtkinter__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:UsersHyojaeAnaconda3libtkinter__init__.py", line 3486, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image


And this is my code sample.



I would appreciate your help.



from tkinter import *

def messageWindow():
win = Tk()
win.geometry('300x200')

root.destroy()


photo2 = PhotoImage(file="img/dog1.gif")
label1 = Label(win, image=photo2)
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)

win.mainloop()

root = Tk()
photo = PhotoImage(file="img/dog2.gif")
label1 = Label(root, image=photo)
label1.pack()


Button(root, text='Bring up Message', command=messageWindow).pack()


root.mainloop()









share|improve this question























  • Try moving the line root.destroy() before win = Tk() to avoid multiple instances of Tk() which may cause problems.

    – acw1668
    Nov 22 '18 at 8:28











  • Or you can specify the Tk instance to be used when initiating the PhotoImage instance: photo2 = PhotoImage(file="img/dog1.gif", master=win).

    – acw1668
    Nov 22 '18 at 8:52
















1












1








1








I want to display the image in a new window, but I get an error.



This is my error code



photo = PhotoImage(file='img/dog')
File "C:UsersHyojaeAnaconda3libtkinter__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:UsersHyojaeAnaconda3libtkinter__init__.py", line 3486, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image


And this is my code sample.



I would appreciate your help.



from tkinter import *

def messageWindow():
win = Tk()
win.geometry('300x200')

root.destroy()


photo2 = PhotoImage(file="img/dog1.gif")
label1 = Label(win, image=photo2)
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)

win.mainloop()

root = Tk()
photo = PhotoImage(file="img/dog2.gif")
label1 = Label(root, image=photo)
label1.pack()


Button(root, text='Bring up Message', command=messageWindow).pack()


root.mainloop()









share|improve this question














I want to display the image in a new window, but I get an error.



This is my error code



photo = PhotoImage(file='img/dog')
File "C:UsersHyojaeAnaconda3libtkinter__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:UsersHyojaeAnaconda3libtkinter__init__.py", line 3486, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image


And this is my code sample.



I would appreciate your help.



from tkinter import *

def messageWindow():
win = Tk()
win.geometry('300x200')

root.destroy()


photo2 = PhotoImage(file="img/dog1.gif")
label1 = Label(win, image=photo2)
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)

win.mainloop()

root = Tk()
photo = PhotoImage(file="img/dog2.gif")
label1 = Label(root, image=photo)
label1.pack()


Button(root, text='Bring up Message', command=messageWindow).pack()


root.mainloop()






python image tkinter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 6:58









이효재이효재

82




82













  • Try moving the line root.destroy() before win = Tk() to avoid multiple instances of Tk() which may cause problems.

    – acw1668
    Nov 22 '18 at 8:28











  • Or you can specify the Tk instance to be used when initiating the PhotoImage instance: photo2 = PhotoImage(file="img/dog1.gif", master=win).

    – acw1668
    Nov 22 '18 at 8:52





















  • Try moving the line root.destroy() before win = Tk() to avoid multiple instances of Tk() which may cause problems.

    – acw1668
    Nov 22 '18 at 8:28











  • Or you can specify the Tk instance to be used when initiating the PhotoImage instance: photo2 = PhotoImage(file="img/dog1.gif", master=win).

    – acw1668
    Nov 22 '18 at 8:52



















Try moving the line root.destroy() before win = Tk() to avoid multiple instances of Tk() which may cause problems.

– acw1668
Nov 22 '18 at 8:28





Try moving the line root.destroy() before win = Tk() to avoid multiple instances of Tk() which may cause problems.

– acw1668
Nov 22 '18 at 8:28













Or you can specify the Tk instance to be used when initiating the PhotoImage instance: photo2 = PhotoImage(file="img/dog1.gif", master=win).

– acw1668
Nov 22 '18 at 8:52







Or you can specify the Tk instance to be used when initiating the PhotoImage instance: photo2 = PhotoImage(file="img/dog1.gif", master=win).

– acw1668
Nov 22 '18 at 8:52














1 Answer
1






active

oldest

votes


















1














You are getting such area because you call root.destroy before the image is loaded to the window.Also you can not use two TK instance you have to use Toplevel check the link to understand better.



Aside that to display image in toplevel you need to create reference for it so it will not be garbage collected Display image in Toplevel window
which i did this way label1.image = sub.



I also use image subsample to demonstrate how to resize image sub = photo2.subsample(5, 5) check this link to read about it



from tkinter import *

def messageWindow():
win = Toplevel()
win.geometry('300x200')

root.withdraw() # THIS HIDE THE WINDOW


photo2 = PhotoImage(file="img/dog1.gif")
sub = photo2.subsample(5, 5)
label1 = Label(win, image=sub)
label1.image = sub
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)



root = Tk()


photo = PhotoImage(file="img/dog1.gif")
sub1 = photo.subsample(3,3)
label1 = Label(root, image=sub1)
label1.pack()


B = Button(root, text='Bring up Message', command=messageWindow)
B.place(x=200, y=300)


root.mainloop()





share|improve this answer


























  • Thank you for the reply. But I have to destroy the original window.

    – 이효재
    Nov 22 '18 at 10:19











  • you cant destroy the root window whilst toplevel is preserved because it is slave to the window to do that you have to use wihtdraw function.

    – AD WAN
    Nov 22 '18 at 10:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • Thank you. Finally solved.

    – 이효재
    Nov 22 '18 at 15:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425425%2ftoo-early-to-create-image-at-newwindow%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You are getting such area because you call root.destroy before the image is loaded to the window.Also you can not use two TK instance you have to use Toplevel check the link to understand better.



Aside that to display image in toplevel you need to create reference for it so it will not be garbage collected Display image in Toplevel window
which i did this way label1.image = sub.



I also use image subsample to demonstrate how to resize image sub = photo2.subsample(5, 5) check this link to read about it



from tkinter import *

def messageWindow():
win = Toplevel()
win.geometry('300x200')

root.withdraw() # THIS HIDE THE WINDOW


photo2 = PhotoImage(file="img/dog1.gif")
sub = photo2.subsample(5, 5)
label1 = Label(win, image=sub)
label1.image = sub
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)



root = Tk()


photo = PhotoImage(file="img/dog1.gif")
sub1 = photo.subsample(3,3)
label1 = Label(root, image=sub1)
label1.pack()


B = Button(root, text='Bring up Message', command=messageWindow)
B.place(x=200, y=300)


root.mainloop()





share|improve this answer


























  • Thank you for the reply. But I have to destroy the original window.

    – 이효재
    Nov 22 '18 at 10:19











  • you cant destroy the root window whilst toplevel is preserved because it is slave to the window to do that you have to use wihtdraw function.

    – AD WAN
    Nov 22 '18 at 10:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • Thank you. Finally solved.

    – 이효재
    Nov 22 '18 at 15:46
















1














You are getting such area because you call root.destroy before the image is loaded to the window.Also you can not use two TK instance you have to use Toplevel check the link to understand better.



Aside that to display image in toplevel you need to create reference for it so it will not be garbage collected Display image in Toplevel window
which i did this way label1.image = sub.



I also use image subsample to demonstrate how to resize image sub = photo2.subsample(5, 5) check this link to read about it



from tkinter import *

def messageWindow():
win = Toplevel()
win.geometry('300x200')

root.withdraw() # THIS HIDE THE WINDOW


photo2 = PhotoImage(file="img/dog1.gif")
sub = photo2.subsample(5, 5)
label1 = Label(win, image=sub)
label1.image = sub
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)



root = Tk()


photo = PhotoImage(file="img/dog1.gif")
sub1 = photo.subsample(3,3)
label1 = Label(root, image=sub1)
label1.pack()


B = Button(root, text='Bring up Message', command=messageWindow)
B.place(x=200, y=300)


root.mainloop()





share|improve this answer


























  • Thank you for the reply. But I have to destroy the original window.

    – 이효재
    Nov 22 '18 at 10:19











  • you cant destroy the root window whilst toplevel is preserved because it is slave to the window to do that you have to use wihtdraw function.

    – AD WAN
    Nov 22 '18 at 10:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • Thank you. Finally solved.

    – 이효재
    Nov 22 '18 at 15:46














1












1








1







You are getting such area because you call root.destroy before the image is loaded to the window.Also you can not use two TK instance you have to use Toplevel check the link to understand better.



Aside that to display image in toplevel you need to create reference for it so it will not be garbage collected Display image in Toplevel window
which i did this way label1.image = sub.



I also use image subsample to demonstrate how to resize image sub = photo2.subsample(5, 5) check this link to read about it



from tkinter import *

def messageWindow():
win = Toplevel()
win.geometry('300x200')

root.withdraw() # THIS HIDE THE WINDOW


photo2 = PhotoImage(file="img/dog1.gif")
sub = photo2.subsample(5, 5)
label1 = Label(win, image=sub)
label1.image = sub
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)



root = Tk()


photo = PhotoImage(file="img/dog1.gif")
sub1 = photo.subsample(3,3)
label1 = Label(root, image=sub1)
label1.pack()


B = Button(root, text='Bring up Message', command=messageWindow)
B.place(x=200, y=300)


root.mainloop()





share|improve this answer















You are getting such area because you call root.destroy before the image is loaded to the window.Also you can not use two TK instance you have to use Toplevel check the link to understand better.



Aside that to display image in toplevel you need to create reference for it so it will not be garbage collected Display image in Toplevel window
which i did this way label1.image = sub.



I also use image subsample to demonstrate how to resize image sub = photo2.subsample(5, 5) check this link to read about it



from tkinter import *

def messageWindow():
win = Toplevel()
win.geometry('300x200')

root.withdraw() # THIS HIDE THE WINDOW


photo2 = PhotoImage(file="img/dog1.gif")
sub = photo2.subsample(5, 5)
label1 = Label(win, image=sub)
label1.image = sub
label1.grid(row=6)

Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)



root = Tk()


photo = PhotoImage(file="img/dog1.gif")
sub1 = photo.subsample(3,3)
label1 = Label(root, image=sub1)
label1.pack()


B = Button(root, text='Bring up Message', command=messageWindow)
B.place(x=200, y=300)


root.mainloop()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 10:25

























answered Nov 22 '18 at 9:29









AD WANAD WAN

8701216




8701216













  • Thank you for the reply. But I have to destroy the original window.

    – 이효재
    Nov 22 '18 at 10:19











  • you cant destroy the root window whilst toplevel is preserved because it is slave to the window to do that you have to use wihtdraw function.

    – AD WAN
    Nov 22 '18 at 10:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • Thank you. Finally solved.

    – 이효재
    Nov 22 '18 at 15:46



















  • Thank you for the reply. But I have to destroy the original window.

    – 이효재
    Nov 22 '18 at 10:19











  • you cant destroy the root window whilst toplevel is preserved because it is slave to the window to do that you have to use wihtdraw function.

    – AD WAN
    Nov 22 '18 at 10:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • @이효재 If solves your problem make sure you tick as a reference for future coders.

    – AD WAN
    Nov 22 '18 at 15:33











  • Thank you. Finally solved.

    – 이효재
    Nov 22 '18 at 15:46

















Thank you for the reply. But I have to destroy the original window.

– 이효재
Nov 22 '18 at 10:19





Thank you for the reply. But I have to destroy the original window.

– 이효재
Nov 22 '18 at 10:19













you cant destroy the root window whilst toplevel is preserved because it is slave to the window to do that you have to use wihtdraw function.

– AD WAN
Nov 22 '18 at 10:33





you cant destroy the root window whilst toplevel is preserved because it is slave to the window to do that you have to use wihtdraw function.

– AD WAN
Nov 22 '18 at 10:33













@이효재 If solves your problem make sure you tick as a reference for future coders.

– AD WAN
Nov 22 '18 at 15:33





@이효재 If solves your problem make sure you tick as a reference for future coders.

– AD WAN
Nov 22 '18 at 15:33













@이효재 If solves your problem make sure you tick as a reference for future coders.

– AD WAN
Nov 22 '18 at 15:33





@이효재 If solves your problem make sure you tick as a reference for future coders.

– AD WAN
Nov 22 '18 at 15:33













Thank you. Finally solved.

– 이효재
Nov 22 '18 at 15:46





Thank you. Finally solved.

– 이효재
Nov 22 '18 at 15:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425425%2ftoo-early-to-create-image-at-newwindow%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft