1

In the process of learning tkinter, I came up against a problem: I can't add an image into a button:

from tkinter import*
from tkinter import ttk

root=Tk()

button=ttk.Button(root)
button.grid()
photo=PhotoImage(file="giphy.gif")
button.config(image=photo, compound=RIGHT)

root.mainloop()

That code makes an error:

<ipython-input-30-6ad3ebb78b5b> in <module>()
      7 button.grid()
      8 photo=PhotoImage(file="giphy.gif")
----> 9 button.config(image=photo, compound=RIGHT)
     10 
     11 root.mainloop()

/usr/lib/python3.5/tkinter/__init__.py in configure(self, cnf, **kw)
   1331         the allowed keyword arguments call the method keys.
   1332         """
-> 1333         return self._configure('configure', cnf, kw)
   1334     config = configure
   1335     def cget(self, key):

/usr/lib/python3.5/tkinter/__init__.py in _configure(self, cmd, cnf, kw)
   1322         if isinstance(cnf, str):
   1323             return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf)))
-> 1324         self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
   1325     # These used to be defined in Widget:
   1326     def configure(self, cnf=None, **kw):

TclError: image "pyimage.." doesn't exist

Why is it so? And how can I fix it?

5
  • 2
    Error does not match code. Please fix. Commented Dec 29, 2017 at 20:32
  • 2
    I tested it and that code works fine for me. Show us some code that reproduces the error. Commented Dec 29, 2017 at 20:34
  • 1
    The same error with other tkinter's components like labels. Commented Dec 29, 2017 at 20:49
  • Are you running this in the jupyter QtConsole? Commented Dec 29, 2017 at 22:18
  • 2
    problem can be bug with PhotoImage and Garbage Collector which removes image from memory when it is assigned to local variable. See Note: on page PhotoImage. Run this code normally in console python script.py , not in Python's shell. Commented Dec 30, 2017 at 1:14

1 Answer 1

1

As furas said in the comment, your code is perfectly runnable with python script.py.

The error comes from the fact you are running it inside Jupyter QtConsole. To be able to run it in the Jupyter QtConsole, you need to explicitly tell tkinter what is the parent window of the PhotoImage. I think this is because in the console, the default parent is not the Tk instance you created but some hidden window. Because of that, the parent of your image is not the parent of the button, so tkinter does not find the image.

The following code should run in the console:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

button = ttk.Button(root)
button.grid()
photo = tk.PhotoImage(file="giphy.gif", master=root)
button.config(image=photo, compound=tk.RIGHT)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.