Image does not open with Tkinter, how to solve?

Asked

Viewed 154 times

1

I need this code to open an image in a label, but this error is returned: "Runtimeerror: Too Early to create image"

from tkinter import *
i = PhotoImage(file="ddddd.png")
root = Tk()
label = Label(root, image=i)
label.pack()
root.mainloop()

ops: The . png file is in the same program folder, running the program by Pycharm and python3

1 answer

2

This error occurs because you try to create the image i = PhotoImage(file="ddddd.png") before it even created the Tk.

To correct, it is quite simple, just reverse the order of the lines as follows, first the root = Tk() and then creating the image i = PhotoImage(file="ddddd.png"), being like this:

root = Tk()
i = PhotoImage(file="ddddd.png")

Your final code will be more or less as follows:

from tkinter import *
root = Tk()
i = PhotoImage(file="ddddd.png")
label = Label(root, image=i)
label.pack()
root.mainloop()

Reference: https://stackoverflow.com/questions/10236857/python-tkinter-error-too-early-to-create-image

  • PERFECT, it worked, thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.