【问题标题】:Tkinter error: image "pyimage2" doesn't exist working with classes and framesTkinter 错误:图像 \"pyimage2\" 不存在使用类和框架
【发布时间】:2023-02-07 00:47:46
【问题描述】:

我正在使用 tkinter 上的课程,我遇到了这个问题:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 549, in _clicked
    self._command()
  File "D:\PYCHARM\pycharmprojects\lumacol_frontend\input_frame.py", line 88, in go_back
    from main import SerialFrame
  File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 126, in <module>
    SerialFrame(root).place(x=25, y=50)
  File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 20, in __init__
    self.createWidgetsMain()
  File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 101, in createWidgetsMain
    refresh_serials = customtkinter.CTkButton(master=self, command=refresh_menu, image=my_image, width=20,
  File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 106, in __init__
    self._draw()
  File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 261, in _draw
    self._update_image()  # set image
  File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 172, in _update_image
    self._image_label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1675, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1665, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist

这是我的应用程序中的代码以及关于它应该如何工作的解释:

首先,我有一个带有 SerialFrame 类的文件,以及窗口和框架的创建:

class SerialFrame(customtkinter.CTkFrame):

# CONSTRUCTOR FOR THE FRAME
def __init__(self, master, *args, **kwargs):
    super(SerialFrame, self).__init__(master)
    self.master = master
    self.serial_port = ""
    self.configure(width=400, height=400)
    self.createWidgetsMain()

# METHOD TO CREATE ALL WIDGETS
def createWidgetsMain(self):
    ...

# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
back = backend.MyAppBackend()
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()

我还有另外 2 个文件,其中包含其他不同类的其他帧,以类似的方式。

问题是当我按下按钮返回第一帧时,这是其他类中的代码:

    def go_back():
        self.destroy()
        btn_back.destroy()
        from main import SerialFrame
        SerialFrame(self.master).place(x=25, y=50)

    btn_back = customtkinter.CTkButton(self.master, text="Go Back",
                                       command=go_back, cursor="hand2")
    btn_back.place(x=465, y=400)

显然,在编写应用程序代码时,我遇到了很多不同的问题,如果您看到一些不应该正常工作的地方,可以告诉我。

我认为错误可能会出现在这里。此代码位于 def createWidgetsMain、主文件和 SerialFrame 类中。

 my_image = customtkinter.CTkImage(light_image=Image.open("images/refresh.png"),
                                          dark_image=Image.open("images/refresh.png"),
                                          size=(20, 20))

        # CREATE REFRESH BUTTON
        refresh_serials = customtkinter.CTkButton(master=self, command=refresh_menu, image=my_image, width=20,
                                                  text="")

我认为当我按下 go_back 按钮时,在其他类上,它应该创建一个新的 SerialFrame 类对象并放置在根目录中。 显然,当我创建其他帧时,我总是发送根 Tk()。

这是创建其他类的按钮代码(在 createWidgedsMain 方法中):

    def segmented_button_callback(value):

        if value == "Inputs":
            self.destroy()
            input_frame.InputFrame(self.master, back).place(x=75, y=75)

        if value == "Menu":
            try:
                connection = back.get_connection()
                self.destroy()
                menu_frame.MenuFrame(self.master, back).place(x=25, y=75)
            except:
                self.destroy()
                SerialFrame(self.master).place(x=25, y=50)

    segemented_button = customtkinter.CTkSegmentedButton(master=self,
                                                         values=["Menu", "Inputs"],
                                                         command=segmented_button_callback)

所有应用程序都运行良好,我唯一的问题是,谢谢。 这是应用程序的一些图片

【问题讨论】:

  • 您是否创建了多个根窗口?您是否在该网站上搜索过确切的错误消息?
  • @BryanOakley 嗨,不,我不创建多个根窗口,如您所见,我只在应用程序开始时创建它并通过方法发送它,我认为这样我不需要创建更多根窗口窗户。
  • @BryanOakley 是的,我已经搜索了很多帖子,但是我看到的任何解决方案都适用于我的代码,因为我已经尝试了其中的很多 :( 这就是我问的原因
  • 我认为我们无法使用您提供的代码 sn-ps 重现这一点。存在缩进错误和遗漏代码。请专门为这个问题创建一个可以重现问题的minimal reproducible example
  • 好的,我做到了github.com/pinkurauchin/examples我看到问题是再次创建一个新的 TK() 但我只想创建一个新的 SerialFrame 对象:(

标签: python tkinter tkinter-button


【解决方案1】:

因为你在main.py里面有下面的代码:

...

# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
back = backend.MyAppBackend()
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()

因此,当执行go_back() 内的from main import SerialFrame 行时,将创建另一个customtkinter.CTk() 实例,这会导致异常。

您需要将代码块放在 if __name__ == "__main__" 块中,如下所示:

...
if __name__ == "__main__":
    # CREATING THE APP
    root = customtkinter.CTk()
    root.geometry("700x500")
    root.title("Lumalcol Conf")
    # CREATING THE FIRST FRAME CALLING THE CLASS MY APP
    SerialFrame(root).place(x=25, y=50)
    root.mainloop()

那么导入main时代码块将不会被执行。

【讨论】:

    猜你喜欢
    • 2014-11-23
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    相关资源
    最近更新 更多