我一直在做一个小项目,遇到了一个错误。作为一个tkinter的新手,我不知道这是什么意思。下面是错误的详细信息和导致它的代码。
这是我的全部错误:
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
字符串
这是我的目录结构:
\assets
err.png
\scripts
windows.py
main.py
型
我的python文件的代码:
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))
型
这确实有效,因为它按预期创建了两个窗口,但其中一个缺少其图像,并且两个窗口都没有自定义图标设置。
预期的操作系统是Windows,但我更喜欢跨平台解决方案。
1条答案
按热度按时间0vvn1miw1#
因为你只是在
b = updateError((500,500))
中传递一个元组(500,500)
到updateError()
,所以它被设置为第一个参数isTk
的值,并将被评估为True
。因此有多个Tk()
的示例导致错误。将
False
作为第一个参数传递:字符串
或者使用
pos
关键字传递元组:型