尝试将简单图像添加到标签中
使用学校提供的 Tkinter 教程网站,我尝试将一个简单的 Paint-drawn gif 添加到我的 Python 程序的标签中。
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
As this returns
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?我该如何解决这个问题?
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()
The snippet in question is:
label = ttk.Label(mainframe).grid(column=1, row=1)
image1 = PhotoImage(file='myimage.gif')
label['image'] = image1
As this returns
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您这样做时
,它会将
Label().grid()
的输出存储在label
中,而不是由Label 创建的新
。Label
实例()则需要这样做。
如果您想将
Label
实例存储在label
中并设置网格,When you do
it's storing the output of
Label().grid()
inlabel
, not the newLabel
instance created byLabel()
.You need to do
if you want to both store the
Label
instance inlabel
and set the grid.