首页 > 解决方案 > Tkinter 照片显示后立即消失

问题描述

嘿,所以我做了这个错误处理程序,如果启用了 2 个特定按钮,它将运行。编码:

def thirderror():
  error2 = Toplevel()
  error2.geometry("250x200")
  error2.configure(bg = "#ffffff")
  canvas2 = Canvas(
      error2,
      bg = "#ffffff",
      height = 200,
      width = 250,
      bd = 0,
      highlightthickness = 0,
      relief = "ridge")
  canvas2.place(x = 0, y = 0)

  errorimg1_resp = requests.get("https://i.postimg.cc/jS8zFCc6/error0.png")
  errorimg1 = ImageTk.PhotoImage(Image.open(BytesIO(errorimg1_resp.content)))
  errorb1 = Button(
      error2,
      image = errorimg1,
      borderwidth = 0,
      highlightthickness = 0,
      command = error2.destroy,
      relief = "flat")

  errorb1.place(
      x = 0, y = 155,
      width = 250,
      height = 45)

  backgroundimg3_resp = requests.get("https://i.postimg.cc/VNVPyTW0/background.png")
  background_img3 = ImageTk.PhotoImage(Image.open(BytesIO(backgroundimg3_resp.content)))
  background3 = canvas2.create_image(
      125.0, 63.0,
      image=background_img3)
  error2.resizable(False, False)

由于某种原因,它显示后,图像(背景和按钮)消失了。此外,我正在使用链接来显示图像,因为我想稍后将其转换为 exe,而不必将图像放在同一目录中。

无论如何,我如何修复图像?

标签: pythonpython-3.xtkinterpython-requests

解决方案


推荐阅读