When using Tkinter, error: TclError: image "pyimage8" doesn't exist - python

I keep getting the error, TclError: image "pyimage8" doesn't exist.
It is strange, as the number increases every time I run it?
I'm running python using spyder, dunno whether this affects anything.
Here is my code:
#import tkinter
import Tkinter as tk
homescreenImage = PhotoImage(file="Homescreen.gif")
#create a GUI window.
root = Tk()
#set the title.
root.title("Welcome to the Pit!")
#set the size.
root.geometry("1100x700")
homescreenFrame = tk.Frame(root, width=1100, height = 700)
homescreenFrame.pack()
homescreenLabel = tk.Label(homescreenFrame, image=homescreenImage)
homescreenLabel.pack()
#start the GUI
root.mainloop()

I found that my script would run once and then give me an error on subsequent runs. If I restarted the console, it would run again. I solved the problem by using the following code in the beginning of my script:
import sys
if "Tkinter" not in sys.modules:
from Tkinter import *
It works every time now.

If you import Tkinter as tk you should use the alias tk when calling tk, eg. root = tk.Tk(). Otherwise Python will not find Tk.
You don't need to import PIL for this.
You can not create a Photoimage before you create Tk.
Try this:
import Tkinter as tk
root = tk.Tk()
root.title("Welcome to the Pit!")
root.geometry("1100x700")
homescreenImage = tk.PhotoImage(file="Homescreen.gif")
homescreenFrame = tk.Frame(root, width=1100, height = 700,)
homescreenFrame.pack()
homescreenLabel = tk.Label(homescreenFrame, image=homescreenImage)
homescreenLabel.pack()
root.mainloop()
Be kind and paste the whole error message in your question also.

Following could be the errors:
1) Give the whole path to the file name
eg: "/home/user/Homescreen.gif"
2) If you are using windows and the above doesn't work:
use "\\C:\\home\\Homescreen.gif" (this is because, windows gets confused)
3) If that also, doesn't work, ensure that the directory of your python
program is the same as that of the image.
4) Also, create the photoimage only after you have created the root
window.
5) For some reason, while running in the debugger, if any previous
executions had thrown errors I get the "pyimage doesn't exist" error.
However, if I restart the debugger (or no previously executed scripts
have thrown errors), then the program runs fine.
6) Also, don't import PIL, it's not required.
Try all the above, if it doesn't work let me know.
Hope this helps.

i think this could be due to :
tkinter only supports .png format for images
Yet, there are other ways to add .gif`` instead of PhotoImage```

In my case, it was because I forgot to keep a reference to the image. Try adding this line after creating the label:
homescreenLabel.image=homescreenImage.

You should use Toplevel window that is directly managed by the window manager.
Just change :
root = Tk() to root = Toplevel()

Related

Python tk - hide console window

I have prepared some tk application. It could be really simple like:
from tkinter import *
# create root window
root = Tk()
# root window title and dimension
root.title("Welcome to GeekForGeeks")
# Set geometry (widthxheight)
root.geometry('350x200')
# all widgets will be here
# Execute Tkinter
root.mainloop()
I have using some method to convert the app to the exe file.
What is important,
I'm not using and I cannot do it with pyinstaller py2exe etc. I also cannot use method with changing my app.py to app.pyw.
But my conversion to .exe is working correctly.
The question - is it even possible to hide/disable/resize(reduce the size) of my console window and make the application still working?
I'm not exactly sure how is it done in pyinstaller py2exe etc, so maybe is it possible to do it inside an application?
All right, to solve above problem install:
pip install pywin32
and add code before running your tk gui application:
import win32gui
import win32.lib.win32con as win32con
the_program_to_hide = win32gui.GetForegroundWindow()
win32gui.ShowWindow(the_program_to_hide , win32con.SW_HIDE)
Then you can run the main.py in console, the console will disappear and the gui app will be still visible.
In case when you use pyinstaller etc - you can convert the application without "--noconsole" argument.
When you run the .exe file the console will appear for a second, and disappear. But the gui app will be still visible and usable.
Hope it help somebody somehow :)
I think you should run your script using pythonw.exe instead of python.exe. See .pyw files in python program
Does this help if using Toplevel?
from tkinter import *
root = Tk()
root.title("Main Window")
root.geometry("200x200")
def launch():
global second
second = Toplevel()
second.title("Child Window")
second.geometry("400x400")
def show():
second.deiconify()
def hide():
second.withdraw()
Button(root, text="launch Window", command=launch).pack(pady=10)
Button(root, text="Show", command=show).pack(pady=10)
Button(root, text="Hide", command=hide).pack(pady=10)
root.mainloop()

When implementing Splash Screen, pyimage doesn't exist anymore

So, that's one of the strangest errors I've ever seen in Python. Hopefully, you can help me.
I have a tkinter mainwindow in main_file implementing images via:
from PIL import Image, ImageTk, ImageColor
#...
self.image_package_render = Image.open('ImagePackage.png')
self.image_package_render_data = np.array(self.image_package_render.convert('RGBA'))
self.plus_render = ImageTk.PhotoImage(self.image_package_render.resize((14, 14), box=(16, 20, 30, 34)))
When I launch the main_file directly, it works like a charm via root.mainloop().
Now I want to implement a splash screen by developing an external script:
import tkinter as tk
splash_root = tk.Tk()
splash_root.geometry("200x200")
splash_label = tk.Label(splash_root, text="Splash Screen", font=18)
splash_label.pack()
import main_file
splash_root.destroy()
tk.mainloop()
Now, however, main_file doesn't start up and throws me the error "_tkinter.TclError: image "pyimage12" doesn't exist".
(Also doesn't work, if I implement the splash_root directly in main_file).
It clearly has something to do with splash_root, but I can't get my head around it.
Cheers guys!
Update: Solved:
It appears that another mainloop apart from root.mainloop() in combination with another window besides a toplevel, just crashes the code somehow.
I implemented a toplevel window as splash like suggested here: Tkinter Show splash screen and hide main screen until __init__ has finished
Thank you!

Tkinter new window declaration

Recently while using python tkinters declaration has not been working. What i mean by this is that a for any file where tkinter has not been imported before, simple code such as creating a window is not possible. Has anyone else encountered this issue? If so, how is it solved?
Any feedback is appreciated. Thanks.
The code is simply supposed to open a window in tkinter. But, when run, no window is displayed.
from tkinter import *
def sample():
window = Tk()
sample()
That's so interesting.
You have to use tkinter.Tk() instead of Tk().
Thanks
Try This :
from tkinter import * # Importing everything from the Tkinter module
win = Tk() # Initializing Tkinter and making a window
def sample():
new_win = Toplevel() # This creates a new window
sample()
win.mainloop() # If this is not there, the code won't work

ttk OptionMenu does not fit into its bounding box

I am facing a problem creating a Tkinter-application under Windows, using python 2.7. Basically, when I create an OptionMenu, its right corner (where a down button indicates that something happens when you click there) is truncated in the middle.
The following code reproduces the issue:
from Tkinter import Tk, StringVar
from ttk import OptionMenu
root = Tk()
options = list('ABC')
var = StringVar(value='A')
om = OptionMenu(root, var, var.get(), *options)
om.config(width=25)
om.pack()
root.mainloop()
The result looks on my computer like this:
I have played around with the padx and ipadx keywords of the packing layout manager and also tried a grid layout instead. None of them lets me see the down-arrow completely.
I appreciate your helpful comments on this issue.
The same happens to me on Windows 7 but not on XP, both using Python 2.7. I have found a bug report which states is should be fixed in Tk 8.5.8. Updating Tcl/Tk in Python seems to be very complicated though
The fix in question is for one of the script files shipped in the tk library. You could modify your local copy of vistaTheme.tcl to match this. In later versions I think it does actually request the size from the system properly but this should work if you are forced to use an older version of Tk.
You can find the path using:
from Tkinter import Tk
tk = Tk()
tk.eval("set tk_library")
and then edit the /ttk/vistaTheme.tcl file. I've got python3 here and it seems to have come with Tk 8.6.1 so has this fixed already.

Tkinter pyimage doesn't exist

I know there are lot of similar questions, but there aren't any simple enough that I am able to understand. I have the following code:
import Tkinter as tk
from PIL import Image, ImageTk
class MainWindow:
def __init__(self, master):
canvas = Canvas(master)
canvas.pack()
self.pimage = Image.open(filename)
self.cimage = ImageTk.PhotoImage(self.pimage)
self.image = canvas.create_image(0,0,image=self.cimage)
filename = full_filename
root = tk.Tk()
x = MainWindow(root)
mainloop()
and I get the following error:
TclError: image "pyimage36" doesn't exist
I've read some stuff about the image objects getting garbage cleaned but I don't quite understand it.
Figured it out. For some reason, while running in the debugger, if any previous executions had thrown errors I get the "pyimage doesn't exist" error. However, if I restart the debugger (or no previously executed scripts have thrown errors), then the program runs fine.
I had the same error message when using spyder 3.3.6 the only way i could get the .png file to load and display after getting the 'Tinker pyimage error ' was to go to the Console and restart the kernel. After that i worked fine.
(Python 3.8)
If you are using a IDE with a console(such as Spyder) just call root.mainloop() in the console.
Odds are that you have a bunch of partially loaded tkinter GUI's that never managed to be executed due to an error that prevented the root.mainloop() function from being run.
Once you have run the root.mainloop() a bunch of GUI's will likely appear on screen. After you have closed all those GUI's try running your code again.
Image of multiple Tkinter GUI's appearing on screen
Read more about mainloop() here: https://pythonguides.com/python-tkinter-mainloop/
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
img = Image.open('Paste the directory path')
bg = ImageTk.PhotoImage(img)
lbl = Label(root, image=bg)
lbl.place(x=0, y=0)
mainloop()
I was getting the same error. Try this code this will help you.
Additionally, in case if you create a button and use it to open other window, then there use window = Toplevel(), otherwise it will again show the same error.
From programmersought
image “pyimage1” doesn’t exist
Because there can only be one root window in a program, that is, only one Tk() can exist, other windows can only exist in the form of a top-level window (Toplevel()).
Original code
import tkinter as tk
window = tk.TK()
Revised code
import tkinter as tk
window = tk.Toplevel()
Keep other code unchanged
https://www.programmersought.com/article/87961175215/

Categories