【问题标题】:Why do I get tkinter.TclError: bitmap not defined error?为什么我会收到 tkinter.TclError: bitmap not defined 错误?
【发布时间】:2021-03-05 19:06:45
【问题描述】:
from tkinter import *
from configparser import ConfigParser
from tkinter import messagebox
import requests

if weather:
    location_lbl['text'] = '{}, {}'.format(weather[0], weather[1])
    image_lbl['bitmap'] = 'icons/{}.png'.format(weather[4])
    temp_lbl['text'] = '{:.2f}°C, {:.2f}°F'.format(weather[2], weather[3])
    weather_lbl['text'] = weather[5]

我收到了这个错误:

_tkinter.TclError: bitmap "icons/50d.png" not defined

请帮帮我。

【问题讨论】:

  • 希望将以下内容标记为答案。

标签: python tkinter bitmap tcl png


【解决方案1】:

这是一个误解,image_lbl['bitmap'] 不是用来显示 png 文件或你加载的图像文件,它更像是显示加载到 tkinter 中的位图:

image_lbl['bitmap'] = 'error' #or questionhead, warning, gray50, etc.

如果要加载 png 图片,请使用tk.PhotoImage,例如:

img = tk.PhotoImage(file='icons/{}.png'.format(weather[4]))
image_lbl['image'] = img

虽然值得注意的是,要使用jpeg,必须使用一个名为PIL 的附加模块。

看看这些有用的链接:

Bitmaps on tkinter

PhotoImage on tkinter

【讨论】:

  • 我强烈建议避免在所有现代界面上使用bitmap 属性。这是 30 年前做事方式的遗留物,至少 25 年来一直与功能无关。
  • @DonalFellows 我同意,它很陈旧。
  • 这将消除错误但运行时不显示图像
  • @SuprabhatKhot 您必须保留对图像的引用,阅读我与图像链接的链接。此 Q 用于您没有显示任何其他图像的 Q。
  • @CoolCloud 好的,我会检查,谢谢顺便说一句
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 2019-06-09
  • 2022-11-16
相关资源
最近更新 更多