繁体   English   中英

为什么我不断收到AttributeError:“模块”对象没有属性

[英]Why do I keep getting AttributeError: 'module' object has no attribute

我是Python的新手,正在尝试一些问题。 这是创建一个交通信号灯。 不知道为什么我不断收到此错误AttributeError:'模块'对象没有属性'画布'

下面的代码

第9章9.25

import tkinter as tk

class TrafficLight(tk.Frame):

    def __init__(self, parent=None):
        tk.Frame.__init__(self, parent, bg='green')
        self.grid()

# create a canvas to draw on

self.canvas = tk.canvas(self, width=260, height=280, bg='white')

self.canvas.grid()


self.make_widgets()


def make_widgets(self):
    self.canvas.create_rectangle(80, 20, 170, 260)

#imagine a square box
# upper left corner coordinates x1, y1
# lower right corner coordinates x2, y2
# and sides of length span for each circle

span = 50
x1 = 100
y1 = 50
x2 = x1 + span
y2 = y1 + span

self.canvas.create_oval(x1, y1, x2, y2, fill='red')
self.canvas.create_oval(x1, y1+70, x1, y2+70, fill='yellow')
self.canvas.create_oval(x1, y1+140, x2, y2+140, fill='green')

if __name__ == '__main__':
    light = TrafficLight()
    light.mainloop()

Python区分大小写。

>>> import tkinter as tk
>>> tk.canvas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'canvas'
>>> tk.Canvas
<class 'tkinter.Canvas'>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM