0

I've been using this as a template:

Blobs Animation

and pretty much the only difference I can find is he uses the line:

from Tkinter import *

whereas I use the line

import Tkinter as Tk

I thought I accounted for this by making all my calls to Tkinter modules as Tk.(Blob.py equivalent). I can this blob animation example to run, but my program gets the error:

'Nonetype' object not callable

on the line:

allStars[i] = allStars[i]()

from my code below (this particular line is in the last block of code):

class Star:

    def __init__(self, canvas, xy, delta):

        self.canvas = canvas
        self.delta = delta

        self.id = self.canvas.create_rectangle(
            xy[0],xy[1],
            xy[0]+starSize,xy[1]+starSize,
            fill='white'
            )


    def __call__(self):
        return self.down

    def down(self):

        xy = self.canvas.coords(self.id)
        if xy[1] >= gameScreenHeight:
            x2 = random.randint(1,gameScreenWidth)
            y2 = 0;
            self.canvas.coords(self.id,x2,y2)
        else:
            self.canvas.move(self.id, 0, self.delta)


root = Tk.Tk()
root.title("Stars")
root.resizable(0,0)
TkPlacement = "%dx%d%+d%+d" % (gameScreenWidth, gameScreenHeight, 10*starSize, heightOffset)
root.geometry(TkPlacement)

frame = Tk.Frame(root, bd = 5, relief=Tk.SUNKEN)
frame.pack()

space = Tk.Canvas(frame, width = gameScreenWidth, height = gameScreenHeight, cursor = 'plus')
space.pack()
space.create_rectangle(0,0,gameScreenWidth,gameScreenHeight,fill="black")



allStars = [
    Star(space, (10,30),3),
    Star(space, (15,60),3),
    Star(space, (80,50),5)
    ]

#N = 99;

#for i in range(N):
#   x1 = random.randint(1,gameScreenWidth)
#   y1 = random.randint(1,gameScreenHeight)
#   aStar = Star(space, (x1,y1), 3)
#   allStars.append(aStar)

root.update()

try:
    while 1:
        for i in range(len(allStars)):
            allStars[i] = allStars[i]()
            root.update_idletasks() # redraw
        root.update() # process events
except Tk.TclError:
    pass # to avoid errors when the window is closed
4

2 回答 2

2

在代码的早期,您设置allStars[i]了一个函数 ( self.done)。该函数None由于没有明确的 return 语句而返回。

当你这样做时:

allStars[i] = allStars[i]()

allStars[i]...您正在用调用的结果替换 的值allStars[i],即None。下一次执行这行代码时,allStars[i]None,所以你得到了那个错误。

如果您尝试制作动画,有比运行自己的无限循环更好的方法,因为您已经运行了无限循环 -- mainloop。例如,您可以使用以下代码删除整个 while 循环和 try/catch 块:

def redraw():
    for i in range(len(items)):
        items[i] = items[i]()
    root.after(int(1000/30), redraw) # 30 frames per second

root.after_idle(redraw)
root.mainloop()
于 2013-05-20T12:45:32.057 回答
1

down()返回None(隐式);第一次通过无限循环,您将allStars用它替换所有内容。

allStars[i] = allStars[i]()可能不是您想要的,尽管尚不清楚您要通过它来完成什么。

于 2013-05-20T12:43:39.180 回答