【问题标题】:_tkinter.TclError: image "..." doesn't exists no matter how I code_tkinter.TclError:无论我如何编码,图像“...”都不存在
【发布时间】:2018-06-24 12:53:05
【问题描述】:

我知道这个问题已经被提出并解决了,购买我仍然遇到同样的问题。

def showpopup(self, event, item):
    global r

    if str(event.type) == "Enter":
        r = tk.Tk()
        r.geometry("400x900+{:}+{:}".format(self.winfo_rootx()+840, self.winfo_rooty()))
        r.wm_title("Pop UP")
        r.overrideredirect(True)
        im = Image.open(os.path.join(images_db_dir, "a.png"))
        imaitem = ImageTk.PhotoImage(im)
        r.columnconfigure(0, weight=1)
        r.rowconfigure(0, weight=1)

        mf = tk.Frame(r)
        mf.grid(row = 0, column=0, sticky="nsew")    

        mf.grid_columnconfigure(0, weight=1)
        f1, f2 = tk.Frame(mf, , tk.Frame(mf, background="red")
        f1.place(x=5, y=5, anchor="nw", width=390, height=390)
        f2.place(x=0, y=405, anchor="nw", width=400, height=400)

        l = tk.Label(f1, image = imaitem)
        l.image = imaitem
        l.grid()
        r.mainloop()

    elif str(event.type) == "Leave":
        r.destroy()

这个程序是一个大程序的一部分,当 event.type 为“Enter”时显示“弹出”(由 tkinter 对象制作),当为“Leave”时将其销毁。在任何情况下,尽管我通过编码 l.image = imaitem 来保留参考,但代码在此之前的行中中断。 当我运行它时,它说图像不存在,即使“弹出”出现并且当标签内编码文本而不是图像时工作正常。

提前致谢。

【问题讨论】:

  • 你确定文件存在吗?
  • 是的,我已经尝试过使用其他图像,并且发生了同样的情况。当我运行主程序时,它可以完美地获取图像,但在运行“弹出窗口”时却没有。
  • 如果 showpopup 从另一个 Tkinter 窗口被调用,你应该使用 Toplevel() 而不是 Tk()。你不应该/不能有两个 Tk() 实例。
  • 非常感谢!现在通过编码 tk.Toplevel() 可以完美地工作,我已经在这里堆叠了一段时间,你让我开心!
  • 很高兴它有帮助。 :) 添加答案只是为了确保这不会保持为 unanswered

标签: python python-3.x image tkinter


【解决方案1】:

如果从另一个 Tkinter 窗口调用 showpopup,您应该使用 Toplevel() 而不是 Tk()。您不能有两个 Tk() 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多