【问题标题】:Image not Displaying even after following effbot [duplicate]即使在关注 effbot [重复] 之后图像也不显示
【发布时间】:2019-10-16 11:55:56
【问题描述】:

我正在尝试将图像添加到屏幕但它不显示。

遵循其他堆栈溢出解决方案和 Effbot 解决方案,但均未奏效。我已经移动了图像,但它仍然没有显示,所以它不在显示的位置。也没有收到任何错误消息。

image = PhotoImage("newspaper-extra-computer-icons-breaking-newsnewspaper.jpg")
image_label = Label(news_aggregator,image = image)
image_label.image = image
image_label.place(x=400,y = 200)

只是希望显示图像。

【问题讨论】:

  • 两件事:第一,您必须将文件名作为关键字参数image=PhotoImage(file="...") 传递。 2、如果需要显示jpg,需要安装PIL,改用ImageTk.Photoimage

标签: python-3.x tkinter


【解决方案1】:

主要问题是您在PhotoImage() 中缺少file=

试试这个:

PhotoImage(file='path_to_image.gif')

也就是说 tkinter 只支持各种格式。

PhotoImage 类可以从文件中读取 GIF 和 PGM/PPM 图像

要使用其他格式,您将需要 PIL。

如果您需要使用其他文件格式,Python 图像库 (PIL) 包含的类可让您加载 30 多种格式的图像,并将它们转换为与 Tkinter 兼容的图像对象:

你可以在here on the documentation那里看到所有东西。

为你的文件试试这个:

import tkinter as tk
from PIL import ImageTk

root = tk.Tk()


image = ImageTk.PhotoImage(file="newspaper-extra-computer-icons-breaking-newsnewspaper.jpg")
image_label = tk.Label(root, image=image)
image_label.image = image
image_label.place(x=400, y=200)

root.mainloop()

请记住,由于您使用place(),上面的代码将不会显示图像,因为它不在框架内。您需要展开窗口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 2022-01-14
    • 1970-01-01
    • 2019-08-21
    • 2018-04-25
    • 2013-03-27
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多