Attempting to add a simple image into a label
Using a Tkinter tutorial site -as supplied by school-, I attempted to add a simple Paint-drawn gif into a La开发者_如何学运维bel 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?
When you do
label = ttk.Label(mainframe).grid(column=1, row=1)
it's storing the output of Label().grid()
in label
, not the new Label
instance created by Label()
.
You need to do
label = ttk.Label(mainframe)
label.grid(column=1, row=1)
if you want to both store the Label
instance in label
and set the grid.
精彩评论