Why do I get tkinter.TclError: bitmap not defined error?
Why do I get tkinter.TclError: bitmap not defined error?
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
请帮帮我。
这是一个误解,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
的附加模块。
看看这些有用的链接:
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
请帮帮我。
这是一个误解,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
的附加模块。
看看这些有用的链接: