“需要超过0个值来解压缩”错误消息

时间:2015-02-13 01:06:26

标签: python

我正在尝试创建一个合并排序动画,它根据分数按升序移动对象。我发布的代码是导致问题的唯一代码段(我相信)。

动画代码:

def moveAnimation(self):
    vx = 20.0
    vy = 20.0
    for i in range (1,len(treasurePointsList)):
        for treasure in treasurePointsList:
            if treasurePointsList[0] == Treasure1.points:
                x,y = canvas.coords(Treasure1)
                canvas.coords(Treasure1,x+vx,y+vy)
                canvas.update()
                x1,y1,x2,y2 = canvas.coords(Treasure1)
                time.sleep(0.1)

整个代码:

from Tkinter import*
import random
import time
global g

main = Tk(className = "sorting animation")
canvas = Canvas(main, width = 700, height = 400, bg = "white")
canvas.pack()
PointsList=[]

class Treasure:
    global g
    def __init__(self,x,y,length,width,colour,canvas,points):
        self.x=x
        self.y=y
        self.length=length
        self.width=width
        self.colour=colour
        self.canvas=canvas
        self.points=points
        self.object=canvas.create_rectangle(self.x,self.y,self.x+self.length,self.y+self.width,fill=self.colour)
        PointsList.append(points)

    def moveAnimation(self):
        vx = 20.0
        vy = 20.0
        for i in range (1,len(treasurePointsList)):
            for treasure in treasurePointsList:
                if treasurePointsList[0] == Treasure1.points:
                    x,y = canvas.coords(Treasure1)
                    canvas.coords(Treasure1,x+vx,y+vy)
                    canvas.update()
                    x1,y1,x2,y2 = canvas.coords(Treasure1)
                    time.sleep(0.1)

def mergeSortAsc(List):
    if len(List) > 1:
        midMA = len(List) // 2
        lHalfMA = List[:midMA]
        rHalfMA = List[midMA:]

        mergeSortAsc(lHalfMA)
        mergeSortAsc(rHalfMA)

        aMA = 0
        bMA = 0
        cMA = 0

        while aMA < len(lHalfMA) and bMA < len(rHalfMA):
            if lHalfMA[aMA] < rHalfMA[bMA]:
                List[cMA] = lHalfMA[aMA]
                aMA += 1

            else:
                List[cMA] = rHalfMA[bMA]
                bMA += 1
            cMA += 1

        while aMA < len(lHalfMA):
            List[cMA] = lHalfMA[aMA]
            aMA += 1
            cMA += 1

        while bMA < len(rHalfMA):
            List[cMA] = rHalfMA[bMA]
            bMA += 1
            cMA += 1

Treasure1 = Treasure(50.0, 160.0, 20.0, 20.0, "red", canvas, 50)
Treasure2 = Treasure(75.0, 160.0, 20.0, 20.0, "yellow", canvas, 1000)
Treasure3 = Treasure(125.0, 160.0, 20.0, 20.0, "blue", canvas, 200)
Treasure4 = Treasure(100.0, 160.0, 20.0, 20.0, "green", canvas, 20)
treasurePointsList=[Treasure1.points,Treasure2.points,Treasure3.points,Treasure4.points]
treasureList=[Treasure1,Treasure2,Treasure3,Treasure4]

treasureList.sort(key=lambda x: x.points,reverse=False)
mergeSortAsc(PointsList)
Treasure3.moveAnimation()

canvas.mainloop()

最后是错误:

Traceback (most recent call last):
  File "C:/Users/Jodie/Documents/Alex/ALL Project/Project 2/Sorting Animation.py", line 91, in <module>
    Treasure3.moveAnimation()
  File "C:/Users/Jodie/Documents/Alex/ALL Project/Project 2/Sorting Animation.py", line 30, in moveAnimation
    x,y = canvas.coords(Treasure1)
ValueError: need more than 0 values to unpack

1 个答案:

答案 0 :(得分:0)

Treasure1似乎是Treasure类的一个实例,它看起来像是在Treasure类本身创建的方法定义中引用的。从长远来看,可能对您有所帮助的是在Treasure1内问自己关于变量Treasure.moveAnimation的预期目的。

可能会向您抛出此错误,因为类中的Treasure1实际上是Treasure.moveAnimation.Treasure1,而不是您稍后在代码中实例化的Treasure1。我的猜测是Treasure.moveAnimation.Treasure1指向什么都没有,所以它给你的错误告诉你Treasure.moveAnimation.Treasure1与None之类的东西相关联。

如果我的解释是正确的,这不是我期望的错误,但是将类的实例的名称放在该类中是非常递归的,并且可能发生足够奇怪的事情,因此错误的确切来源将是很难找到。

我的建议是找到一种方法来做你想做的事情,而不需要在类定义中引用类的实例。