在我的代码脚本中,我打开了一个窗口,其中应该显示爬行者头部的图像。每当我尝试运行代码时,都会收到此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/opt/homebrew/Cellar/[email protected]/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/tkinter/__init__.py", line 1967, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.12/site-packages/customtkinter/windows/widgets/ctk_button.py", line 554, in _clicked
self._command()
File "/Users/*me*/Desktop/Python/Computer Science 20 Stuff/Cookie Clicker Game/main.py", line 380, in info_button_callback
new_window()
File "/Users/*me*/Desktop/Python/Computer Science 20 Stuff/Cookie Clicker Game/main.py", line 143, in new_window
creeper_label = customtkinter.CTkLabel(
^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.12/site-packages/customtkinter/windows/widgets/ctk_label.py", line 104, in __init__
self._update_image()
File "/opt/homebrew/lib/python3.12/site-packages/customtkinter/windows/widgets/ctk_label.py", line 141, in _update_image
self._label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
File "/opt/homebrew/Cellar/[email protected]/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/tkinter/__init__.py", line 1721, in configure
return self._configure('configure', cnf, kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/tkinter/__init__.py", line 1711, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage26" doesn't exist
我已经验证它与 python 文件位于同一文件路径中,我已经使用另一个图像更改了图像,我可以在代码中出于不同目的加载该图像,并且我已经检查了文件扩展名,它是合适的.
代码(简称):
import customtkinter
from PIL import Image
creeper_bg = customtkinter.CTkImage(
dark_image=Image.open("creeper_bg.png"), size=(70, 70))
def main_loop():
* Main stuff here*
def back_right_button_callback():
* back_right_button_callback stuff here *
def info_button_callback():
info_window = customtkinter.CTk()
w = 300
h = 200
ws = info_window.winfo_screenwidth()
hs = info_window.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
info_window.geometry('%dx%d+%d+%d' % (w, h, x, y))
info_window.resizable(False, False)
creeper_label = customtkinter.CTkLabel(
info_window, width=70, height=70, image=creeper_bg, text="", corner_radius=0, fg_color="transparent", bg_color="transparent")
creeper_label.place(x=10, y=100, anchor="w")
info_window.mainloop()
info_button = customtkinter.CTkButton(main, image=info_bg, text="", width=40, height=40, fg_color="transparent", bg_color="transparent", corner_radius=0, border_width=0, border_spacing=0, hover=True, hover_color="#868686", command=info_button_callback)
info_button.place(x=46, y=576, anchor="w")
弄清楚出了什么问题,我只需要
info_window
成为 info_window = customtkinter.CTkToplevel()
而不是 info_window = customtkinter.CTk()