加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – _tkinter.TclError:位图“pyimage2”未定义

发布时间:2020-12-20 13:48:42 所属栏目:Python 来源:网络整理
导读:我想在菜单栏上显示一个图标,所以使用这个 information,我编码: img = Image.open("help.png") menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img)) 我收到了这个错误: Traceback (most recent call last): File "mine.py",l
我想在菜单栏上显示一个图标,所以使用这个 information,我编码:

img = Image.open("help.png") 
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))

我收到了这个错误:

Traceback (most recent call last):
  File "mine.py",line 67,in <module>
    m.menus(root)
  File "mine.py",line 55,in menus
     menubar.add_cascade(label="Help",bitmap=ImageTk.PhotoImage(img))
  File "/usr/lib/python2.7/lib-tk/Tkinter.py",line 2699,in add_cascade
    self.add('cascade',cnf or kw)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py",line 2696,in add
    self._options(cnf,kw))
_tkinter.TclError: bitmap "pyimage2" not defined

如何解决这个问题?

解决方法

要显示PhotoImage,您应该使用image属性,而不是位图.
您也可以直接使用ImageTk.PhotoImage打开图像文件(file =’…’)

因此,您可以使用以下代码在菜单中显示图像:

img = ImageTk.PhotoImage(file="help.png") 
menubar.add_cascade(label="Help",image=img)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读