尝试将简单图像添加到标签中 [英] Attempting to add a simple image into a label

查看:23
本文介绍了尝试将简单图像添加到标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Tkinter 教程站点(由学校提供),我尝试将一个简单的 Paint-drawn gif 添加到我的 Python 程序的标签中.

Using a Tkinter tutorial site -as supplied by school-, I attempted to add a simple Paint-drawn gif into a Label on my Python program.

mainframe = ttk.Frame(sub, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe['padding'] = (5,10)
label = ttk.Label(mainframe).grid(column=1, row=1)
image1 = PhotoImage(file='myimage.gif')
label['image'] = image1
sub.mainloop()

有问题的片段是:

label = ttk.Label(mainframe).grid(column=1, row=1)
image1 = PhotoImage(file='myimage.gif')
label['image'] = image1

当它返回时

Exception in Tkinter callback
Traceback (most recent call last):
    File "C:\Python31\lib\tkinter\__init__.py", line 1399, in __call__
        return self.func(*args)
    File "\\curriculum.lan\filestore\home\2005\jasomner\Downloads\trial5.py", line 20, in subwindow
        label['image'] = image1
TypeError: 'NoneType' object does not support item assignment

为什么?我该如何解决这个问题?

Why? How can I fix this?

推荐答案

什么时候做

label = ttk.Label(mainframe).grid(column=1, row=1)

它将Label().grid() 的输出存储在label 中,而不是由Label 创建的新Label 实例().

it's storing the output of Label().grid() in label, not the new Label instance created by Label().

你需要做的

label = ttk.Label(mainframe)
label.grid(column=1, row=1)

如果你想同时在 label 中存储 Label 实例并设置网格.

if you want to both store the Label instance in label and set the grid.

这篇关于尝试将简单图像添加到标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆