这个AttributeError意味着什么? 'int'对象没有属性'items'

时间:2016-04-16 07:09:05

标签: python python-3.x tkinter tkinter-canvas

我的函数“draw_pieces()”有问题,因为当我运行我的代码时,我有以下错误:(没有“draw_pieces()”我的代码工作)

_cnfmerge: fallback due to: 'int' object is not iterable
Traceback (most recent call last):
  File "C:\Users\Thierry\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 102, in _cnfmerge
    cnf.update(c)
TypeError: 'int' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Thierry\Desktop\Chess\Chessboard.py", line 31, in <module>
    draw_pieces()
  File "C:\Users\Thierry\Desktop\Chess\Chessboard.py", line 28, in draw_pieces
    canvas.create_image=Canvas(30,30, image=photo, anchor=CENTER, state=NORMAL)
  File "C:\Users\Thierry\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2244, in __init__
    Widget.__init__(self, master, 'canvas', cnf, kw)
  File "C:\Users\Thierry\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2129, in __init__
    cnf = _cnfmerge((cnf, kw))
  File "C:\Users\Thierry\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 105, in _cnfmerge
    for k, v in c.items():
AttributeError: 'int' object has no attribute 'items'

这是我的代码:

from tkinter import *
root=Tk()
root.geometry("512x512")

rows = 8
columns = 8
color1 = "#b35821" #Flery Orange
color2 = "#efcb9d" #New Tan
dim_square = 64

canvas=Canvas(root, width=512, height=512)
canvas.pack()

photo=PhotoImage(file="blackk.gif")

def draw_chessboard():
    color = color2
    for r in range(rows):
        color = color1 if color == color2 else color2
        for c in range(columns):
            x1 = (c * dim_square)
            y1 = ((7-r) * dim_square)
            x2 = x1 + dim_square
            y2 = y1 + dim_square
            canvas.create_rectangle(x1, y1, x2, y2, fill=color, tags="area")
            color = color1 if color == color2 else color2

def draw_pieces():
    canvas.create_image=Canvas(30,30, image=photo, anchor=CENTER, state=NORMAL)

draw_chessboard()
draw_pieces()

root.mainloop()
你可以帮我解决这个问题吗? 在此先感谢您的帮助:)。

1 个答案:

答案 0 :(得分:1)

我尝试进行此更改,程序运行时没有错误:

原创

canvas.create_image=Canvas(30,30, image=photo, anchor=CENTER, state=NORMAL)

更改为:

canvas.create_image(30,30, image=photo, anchor=CENTER, state=NORMAL)

当我使用tkinter时,我也有这个资源。我希望这会有所帮助。

http://infohost.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf