You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCharm中easygui结合matplotlib出现pyimage19不存在错误求助

问题:PyCharm中easygui调用enterbox报错_tkinter.TclError: image "pyimage19" doesn't exist

我查了大量同类问题都没能解决。我的需求是加载motion文件并绘图,再通过easygui在GUI中展示相关内容。这段代码在Jupyter Notebook和Spyder中运行完全正常,但在PyCharm里持续抛出错误:_tkinter.TclError: image "pyimage19" doesn't exist。

完整代码

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")

排查过程

  • 使用目录中已有的pupil.png时,第一个enterbox(st_A)能正常弹出,但第二个enterbox(st_B)报错
  • 移除以下绘图相关代码段后,两个enterbox都能正常运行:
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)
  • 确认pupil.png文件存在且无损坏,推测是easygui与PyCharm的兼容问题

完整报错回溯

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

解决方法

这个错误的核心原因是Matplotlib的Tkinter后端与easygui的Tk实例冲突:Matplotlib绘图时会创建独立的Tk主窗口,后续easygui复用这个窗口上下文时,之前加载的图片对象已被回收或上下文丢失,导致找不到图片资源。以下是三种可行的解决方式:

方式1:指定Matplotlib使用无GUI后端

如果不需要Matplotlib的交互式窗口,仅需保存图片,可在导入matplotlib后强制使用Agg后端,避免创建Tk窗口:

import matplotlib
matplotlib.use('Agg')  # 放在import matplotlib.pyplot之前
import matplotlib.pyplot as plt

方式2:手动清理Matplotlib绘图资源

在保存图片后,手动关闭所有Matplotlib图形窗口,释放相关Tk资源:

fig12.savefig(save_direction)
# 添加以下两行代码
plt.close(fig12)  # 关闭当前绘制的图形
plt.close('all')  # 关闭所有Matplotlib窗口

方式3:统一Tk实例

如果需要保留Matplotlib的交互式窗口,可提前初始化统一的Tk主实例,让easygui复用该实例:

import tkinter as tk
# 在所有easygui和Matplotlib操作前执行
root = tk.Tk()
root.withdraw()  # 隐藏主窗口,不影响GUI操作

# 后续原有代码保持不变

内容的提问来源于stack exchange,提问作者Faezeh Rabbani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 10:20:38