[PYTHON] I get "too early to create image" on tkinter

TL;DR Let's execute Tk () first

Status

When I was trying to make a GUI program using tkinter, I tried to display an image on canvas, and when I wrote it, I got an error, so I tried to check the contents with ipython.

ipython


from PIL import Image, ImageTk
import tkinter as tk

img = Image.open("hoge.png ")
imgtk = ImageTk.PhotoImage(img)

Then

RuntimeError: Too early to create image

I couldn't move on

Cause

PhotoImage () cannot be used unless tk.Tk () is called first.

Coping

When I was trying to parse with ipython, it became like this because tk.Tk () in the original script was not reflected. The error that was originally in trouble was in the image, so I was confused and the discovery was delayed. On ipython, if you call root = tk.Tk () and Tk () appropriately, there is no problem after that.

Recommended Posts

I get "too early to create image" on tkinter
I tried to get an image by scraping
I want to drop a file on tkinter and get its path [Tkinter DnD2]
Create Image Viewer with Tkinter
I get [Error 2055] when trying to connect to MySQL on Heroku
How to create an ISO file (CD image) on Linux
I tried to create a server environment that runs on Windows 10
I tried to create an environment of MkDocs on Amazon Linux
I tried to create Quip API
I get an ImportError on appengine_config.py
Get only image tweets on twitter
I tried to get started with Bitcoin Systre on the weekend
I tried to get the batting results of Hachinai using image processing
I want to display an image on Jupyter Notebook using OpenCV (mac)
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
What I referred to when studying tkinter
I tried to get started with Hy
I want to use Linux on mac
I was addicted to Flask on dotCloud
Program to get favorite images on Twitter
I want to develop Android apps on Android
I made an image classification model and tried to move it on mobile
I want to easily create a Noise Model
I want to create a window in Python
I tried to get CloudWatch data with Python
I tried to correct the keystone of the image
I want to get League of Legends data ③
Try to create a new command on linux
I want to create a plug-in type implementation
I made a tool to get new articles
I want to log file I / O on Linux
I want to get League of Legends data ①
Create a Python execution environment on IBM i
Create a command to get the work log
I tried playing with the calculator on tkinter
[Pyhton] I want to solve the problem that tkinter does not work on MacOS11
When I try to import pandas on macOS I get the error No module named'_bz2'
I get an error when I try to raise Python to 3 series using pyenv on Catalina