builtins.AttributeError:'int'对象没有属性'create_line'

时间:2014-10-31 18:44:26

标签: python python-3.x tkinter int attributeerror

我有这个图形代码,用于绘制不同的点。但是,当我为x轴设置我的数字行时,我一直收到一个int错误。

from tkinter import *

def checkered(canvas, line_distance):
   for x in range(line_distance,canvas_width,line_distance):
      canvas.create_line(x, 0, x, canvas_height, fill="#476042")
   for y in range(line_distance,canvas_height,line_distance):
      canvas.create_line(0, y, canvas_width, y, fill="#476042")
###############################################################################
root = Tk()
canvas_width = 1000
canvas_height = 760
w = Canvas(root, 
           width=canvas_width,
           height=canvas_height)
checkered(w,20)           
w.pack()

a = int(canvas_height / 2)
w.create_line(0, a, canvas_width, a, 
               fill="black",width=3)
b = int(canvas_width / 2)
w.create_line(b, 0, b,canvas_height, 
              fill = "red",width=3)
w.create_text(5, ((canvas_height/2)-10)
              ,text="x",font=12)
w.create_text(b+10 ,5
              ,text='y',font=12)
w.create_text(b+10,a-10
              ,text='0',font=12)
w.create_oval(b-5,a-5,b+5,a+5 
              ,fill = 'black')
w=int(canvas_width/50)
z=int(canvas_width/100)
aa=b+w
bb=a-z
cc=a+z 
w.create_line(aa,bb,aa,cc,
              fill='black',width=3)

mainloop()

每当我运行程序时,我都会收到此消息:

line 37, in <module>
builtins.AttributeError: 'int' object has no attribute 'create_line'

这对我来说没有意义,因为'int'在第19行和第22行工作:

a = int(canvas_height / 2)
w.create_line(0, a, canvas_width, a, 
               fill="black",width=3)

b = int(canvas_width / 2)
w.create_line(b, 0, b,canvas_height, 
              fill = "red",width=3)

这些线条工作正常,只是最后一行不能正常工作。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您将w设置为整数:

w=int(canvas_width/50)
z=int(canvas_width/100)
aa=b+w
bb=a-z
cc=a+z 

然后在下一行仍然期望它是Canvas

w.create_line(aa,bb,aa,cc,
              fill='black',width=3)

使用更好的变量名称;例如,使用canvas而不是w,您在重复使用名称时不会轻易被欺骗。