image "pyimage19" doesn't exist

294 views Asked by At

I find several questions with the same topics but non of them solve my problem.

I want to load a file (motion) and plot a figure base on that and show the figure on a GUI using easygui. I used this code on jupyter notebook and spyder and there was no problem. now I am using Pycharm and continuously get this error: "self.tk.call( _tkinter.TclError: image "pyimage19" doesn't exist"

Here is complete code:

import numpy as np
import matplotlib.pyplot as plt
import os.path
import easygui
from easygui import enterbox

Base_path = easygui.diropenbox(title='select folder contaning data')
file_name1 = "Results12"
save_direction1 = os.path.join(Base_path, file_name1)
os.mkdir(save_direction1)

Base_path2 = os.path.join(Base_path + "\suite2p" + "\plane0")
facemap = easygui.fileopenbox(title='Select facemap results for this session')
facemap = np.load(facemap, allow_pickle=True)
motion = facemap.item()['motion']
motion = motion[1]

text2 = "start"
title2 = ""
st_A = enterbox(text2, title2, 0, image="pupil.png")

TIM = np.arange(0, len(motion))
fig12 = plt.figure(figsize=(2, 13))
plt.plot(TIM, motion)

file_name = "raw_face_motion.png"
save_direction = os.path.join(save_direction1, file_name)
fig12.savefig(save_direction)


text2 = "end"
title2 = ""
st_B = enterbox(text2, title2, 0, image="pupil.png")

To check the problem instead of using plotted image(fig12) I used an image that I already had in the directory "pupil.png" Then something weird happens "st_A" enterbox works and pop up but "st_B" enterbox is not working and give me the error. if I remove this part of the code:

TIM = np.arange(0, len(motion))
fig12 = plt.figure(figsize=(2, 13))
plt.plot(TIM, motion)

file_name = "raw_face_motion.png"
save_direction = os.path.join(save_direction1, file_name)

both enterboxes will works without problems.

the image exists and there is no problem with that and the same code works on Jupyter Notebook and spyder I think there must be something with easygui and Pycharm. I would appreciate it if you could help me with what is the problem.

Complete traceback of error:

C:\Users\User\PycharmProjects\PostSuite2p\venv\Scripts\python.exe C:\Users\User\PycharmProjects\PostSuite2p\main.py 
Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\PostSuite2p\main.py", line 33, in <module>
    st_B = enterbox(text2, title2, 0, image="pupil.png")
  File "C:\Users\User\PycharmProjects\PostSuite2p\venv\lib\site-packages\easygui\boxes\derived_boxes.py", line 361, in enterbox
    result = __fillablebox(
  File "C:\Users\User\PycharmProjects\PostSuite2p\venv\lib\site-packages\easygui\boxes\fillable_box.py", line 73, in __fillablebox
    label = tk.Label(imageFrame, image=tk_Image)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 3148, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2572, in __init__
    self.tk.call(
_tkinter.TclError: image "pyimage19" doesn't exist
1

There are 1 answers

0
acw1668 On

It is because calling plt.figure(...) will create an instance of Tk implicitly. Another instance of Tk will also be created when st_B = enterbox(...) is executed which causes the exception due to multiple instances of Tk.

You need to call matplotlib.use('Agg') to disable creation of Tk implicitly by matplotlib module as below:

...
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
...