【问题标题】:Python tkinter PhotoImage not working well?Python tkinter PhotoImage 不能正常工作?
【发布时间】:2020-08-22 05:25:46
【问题描述】:

为什么我的 Tkinter 图像没有出现?

我写了这段代码

from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog

root = Tk()


def ouvrir():
    global image
    root.filename = filedialog.askopenfilename(initialdir="\Khaoula\Desktop\open_cv_book", title="Select a file",filetypes=(("jpg files", "*.jpg"), ("all files", "*.*")))
    image = ImageTk.PhotoImage(Image.open(root.filename))


button = Button(root, text="Open File", command=ouvrir)
button.pack()

root.mainloop()

没有错误
但是图片没有显示出来

你能帮忙吗?

【问题讨论】:

  • 您只是创建了一个ImageTk.PhotoImage 的实例,但不要在任何LabelCanvas 中使用它。
  • 是的,你是对的!太谢谢你了

标签: python tkinter python-imaging-library


【解决方案1】:

根据您上面给出的代码: 您只是在创建该图像的一个实例。 如果您希望窗口显示图像,那么您应该在标签/按钮/画布上调用它。

希望对您有所帮助!

【讨论】:

  • 是的,对,你是的,对,你
猜你喜欢
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
相关资源
最近更新 更多