r/learnpython 1d ago

Help making image appear in easygui msgbox

hi there - i'm trying to add a small image to an easygui msgbox, but i think i'm probably not importing it properly. what i have at the beginning is:

import easygui
image_1 = "skull_tiny.png"

and then a bunch of stuff (it's a short choose-your-own-adventure story) and then i would like to add the image at the point the player dies, like so:

if answer5 == "turn":
                easygui.msgbox("You choose to turn and find yourself face to face with an enormous dinosaur. The last thing you see before you die is two rows of sharp teeth coming towards you from above.", image = image_1, ok_button = "Game over.")

the code works fine without the image, but as soon as i add it, all that comes up when you get to that part is a small empty box, with no clickable options. the image is in the same folder as the .py file, but maybe i need to put in the full path?

this is what's in the console:

File ~\AppData\Local\spyder-6\envs\spyder-runtime\Lib\tkinter__init__.py:1711, in Misc._configure(self, cmd, cnf, kw)
   1709 if isinstance(cnf, str):
   1710     return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf)))
-> 1711 self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))

TclError: image "pyimage4" doesn't exist

any help would be greatly appreciated!

0 Upvotes

5 comments sorted by

2

u/socal_nerdtastic 1d ago

Hmm this is an error that typically happens when you run several tkinter.Tk() instances at once. Are you using a tkinter window as well as easygui?

Also please tell us what version of python are you using and how exactly did you install easygui?

1

u/greenleafsalad 1d ago

thanks for replying! afaik i wasn't using a tkinter window, but i could be mistaken. i was using the same install of spyder that i installed last year for a programming class, and i can't remember how i originally installed easygui, but i ran the program from vs code and it works now, so maybe it had something to do with that?

for now, problem solved. in the future maybe there'll come a time where i'll want/need to understand exactly what was wrong, but for right now that's above my pay grade 🥲

1

u/socal_nerdtastic 1d ago

Glad you have it solved. TBH I'll bet it's an old bug in easygui that they've fixed in the meantime.

1

u/woooee 1d ago

TclError: image "pyimage4" doesn't exist

This usually means that there is no reference to the image. Show how you opened the image.

EasyGUI is a subset of tkinter. In tkinter you use "compound" to combine text and image (https://dafarry.github.io/tkinterbook/label.htm and search for "compound"). See if EasyGUI has something similar.

Here is something done in tkinter that you can run. Note that the code below uses "data=" instead of "image=" because it is included as data in the program itself instead of opening and loading an image file.

import tkinter as tk

root = tk.Tk()
root.geometry("200x150")

# An image used for testing
ladybug_gif_b64='''\
R0lGODlhIAAgALMAAP///wAAADAwMP99Hf8AAP+lAP//AMopUwAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAACH5BAAAAAAALAAAAAAgACAAAwTHEMhJq714hp3lDh0GiqH2UWOVAt96pUIsBLKglWg87Dwv
4xMBj0Asxgg+XKxwLBJrxUGsI5TKnARoVHoLDp5XbNP5cwmNAqwa/aOc13ByrfKOw2UGw1SSxrb+
AWIxeXsAaX92UDQ1Nh6BdneMhQIHkHGSjYaVlmt4epmacp19YAKEoJRspKWrjBapWWGqcm1uB5tj
ok4HZa+3m5wEt5kuhpTAcb+FVL/NzspAfDHPysslMJjEIS0oLygnOMVd0SwcHeDk6errEQA7
'''
image=tk.PhotoImage(data=ladybug_gif_b64)

std_font = ("Arial", 15, "bold")
# Create a Label with text over the image
tk.Label(root, text="Text Overlay", image=image,
                 compound='center', fg="black",
                 font=std_font).grid(row=0)

# Create a Label with image to the right of text
tk.Label(root, text="Image Right ", image=image,
                 compound='right', fg="black",
                 font=std_font).grid(row=1)

tk.Button(root, text="Quit", bg="orange", font=std_font,
          command=root.quit).grid(row=99, sticky="nsew")

root.mainloop()