Issue
I've been working on a small project, and have encountered an error. Being a somewhat novice in tkinter, I don't know what it means. The details of the error and code that caused it are below.
This is my full error:
Traceback (most recent call last):
File "D:\red zone\main.py", line 5, in <module>
b = updateError((500,500))
File "D:\red zone\scripts\windows.py", line 44, in __init__
self.widget["image"] = self.widget.errorImage
File "C:\Users\maude_u0wvgig\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1727, in __setitem__
self.configure({key: value})
File "C:\Users\maude_u0wvgig\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1716, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\maude_u0wvgig\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1706, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
This is my directory structure:
\assets
err.png
\scripts
windows.py
main.py
My python files' code:
windows.py
## imports
import base64
import os
import tempfile
import time
import tkinter as tk
import zlib
from tkinter import *
from tkinter import ttk
class updateError():
def __init__(self, isTk=False, pos=[500,500]):
## variables
self.ICON = zlib.decompress(base64.b64decode('eJxjYGAEQgEBBiDJwZDBy'
'sAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4JRMApGwQgF/ykEAFXxQRc='))
## main program
# window setup
self._, self.ICON_PATH = tempfile.mkstemp()
with open(self.ICON_PATH, 'wb') as self.icon_file:
self.icon_file.write(self.ICON)
if isTk:
self.root = Tk()
elif not isTk:
self.root = Toplevel()
elif type(isTk) is not bool:
raise TypeError(f"needs bool, not {type(isTk)}")
if not 0 <= pos[0] <= 900:
raise ValueError("x position must be between 0 and 900")
elif not 0 <= pos[1] <= 750:
raise ValueError("y position must be between 0 and 750")
self.frm = ttk.Frame(self.root, padding=10)
self.frm.grid()
# window content
ttk.Label(self.frm, text="Windows was not installed properly. Please reinstall Windows.\nError 4 (Windows error 096)").grid(column=1, row=0)
ttk.Button(self.frm, text="Ok").grid(column=5, row=3)
# window config
self.root.geometry(f"+{pos[0]}+{pos[1]}")
self.root.title("Microsoft Windows")
self.root.resizable(width=False, height=False)
self.root.iconbitmap(default=self.ICON_PATH)
self.widget = ttk.Label(self.frm)
self.widget.errorImage = tk.PhotoImage(file=r"assets\err.png")
self.widget["image"] = self.widget.errorImage
self.widget.grid(column=0, row=0)
self.root.after(1, lambda: self.root.focus_force())
main.py
## imports
from scripts.windows import updateError
a = updateError(True, (900,750))
b = updateError((500,500))
This does work in that it creates both windows as intended, but one is missing its image and both do not have the custom icons set.
Intended OS is Windows, but I would prefer cross-platform solutions.
Solution
Since you just pass a tuple (500,500)
to updateError()
in b = updateError((500,500))
, so it is set as the value of the first argument isTk
and will be evaluated as True
. Therefore there are multiple instances of Tk()
which causes the error.
Pass False
as the first argument instead:
b = updateError(False, (500,500))
Or use pos
keyword to pass the tuple:
b = updateError(pos=(500,500))
Answered By - acw1668
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.