为什么要获取tkinter.TclError:位图未定义错误?

2024-05-16 00:40:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

请帮帮我


Tags: textfromimportformatifpngtkinterrequests
1条回答
网友
1楼 · 发布于 2024-05-16 00:40:22

这是一个误解,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

相关问题 更多 >