I hope you are not tired of me posting about this, but I'm determined to 
understand what I'm doing wrong, and that means I'm pounding away, searching 
and not able to fix it or find what the problem is.

I've written the class to make the .gif move.  Now, this did work (to a point) 
when I was writing it as more of a script. I could get the .gif to move from 
one place to another, only to start at the same place again.  That is why the 
class became important, because I don't believe in globals. With my latest 
changes I get an error.

I've attached the latest code.  The problem is at the bottom where I'm trying 
to bind an event to <Button-1>.  This is the error message I get:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
    return self.func(*args)
TypeError: unbound method direction() must be called with Images instance as 
first argument (got Event instance instead)

I've done some searching in the Greyson book (finally downloaded it) and can't 
seem to see what's wrong.  I also did a search on the 'TypeError:'  which I 
find funny because I thought Python wasn't strongly typed.  But I suppose that 
this is a simple error that one of you incredibly bright people will look at 
and I will respond with a 'Doh' (and a slap to the forehead). At least I hope 
so!

Thanks

T
"""
Build the maze and pathway
"""
from Tkinter import *
import time

root = Tk()
root.title("Background")
canvasOne = Canvas(width = 800, height = 700, bg = 'black')
canvasOne.pack()

def createWall((x0, y0), (x1, y1), colour = 'blue', width = 3):
    """ create a double wall outlining the maze. """
    canvasOne.create_line(x0, y0, x1, y1, width = width, fill = colour)
    canvasOne.create_line(350, 630, 450, 630, width = 3, fill = 'gold', tag = 
'gate')

def buildPathHorizontal((val1, val2, number), increment = 20, delta = 5):
    """ build the  horizontal path through the maze, small white squares"""
    for i in range(number):
        val1 += increment
        x, y = val1, val2
        deltax = x + delta
        deltay = y + delta
        canvasOne.create_rectangle(x, y, deltax, deltay, fill = 'white')

def buildPathVertical((val1, val2, number), increment = 20, delta = 5):
    """ build the vertical path through the maze, small white squares"""
    for i in range(number):
        val2 += increment
        x, y = val1, val2
        deltax = x + delta
        deltay = y + delta
        canvasOne.create_rectangle(x, y, deltax, deltay, fill = 'white')
    
outerWall = [(450, 640), (475, 640), (475, 640), (475, 690), (475, 690), (790, 
690), (790, 690),
                 (790, 530), (790, 530), (660, 530), (660, 530), (660, 360), 
(790, 360), (790, 10), (790, 10),
                 (10, 10), (10, 10), (10, 360), (10, 360), (150, 360), (150, 
360), (150, 530), (150, 530),
                 (10, 530), (10, 530), (10, 690), (10, 690), (325, 690), (325, 
690), (325, 640), (325, 640),
                 (350, 640), (350, 630), (350, 630), (315, 630), (315, 680), 
(20, 680), (20, 680), (20, 560),
             (20, 540), (160, 540), (160, 540), (160, 350), (20, 350), (20, 
350), (20, 20), (20, 20),
             (380, 20), (380, 20), (380, 130), (380, 130), (420, 130), (420, 
130), (420, 20), (420, 20),
             (780, 20), (780, 350), (780, 350), (650, 350), (650, 350), (650, 
540), (650, 540), (780, 540),
             (780, 540), (780, 680), (780, 680), (485, 680), (485, 630), (485, 
630), (450, 630), (450, 630),
             (450, 640)]
topLeftBox = [(130, 105), (130, 125), (130, 125), (250, 125), (250, 125), (250, 
105),
                    (130, 105), (250, 105)]
secondTopLeftBox = [(160, 215), (160, 225), (160, 215), (230, 215), (230, 215),
                    (230, 225), (160, 225), (230, 225)]
topRightBox = [(545, 105), (545, 125), (545, 125), (665, 125), (665, 125), 
(665, 105), 
               (545, 105), (665, 105)]
secondTopRightBox = [(560, 215), (560, 225), (560, 215), (625, 215), (625, 
215), (625, 225),
                     (625, 225), (560, 225)]
middleT = [(345, 240), (455, 240), (345, 240), (345, 240), (345, 250), (395, 
250), (395, 250), (395, 335),
           (395, 335), (405, 335), (405, 335), (405, 250), (405, 250), (455, 
250), (455, 250), (455, 240)]
leftSidewaysT = [(275, 340), (275, 500), (275, 340), (285, 340), (285, 340), 
(285, 415), (285, 415), (345, 415),
                 (345, 415), (345, 425), (285, 425), (345, 425), (285, 425), 
(285, 500), (275, 500), (285, 500)]
rightSidewaysT = [(525, 340), (525, 500), (525, 340), (515, 340), (515, 340), 
(515, 415), (515, 415), (455, 415),
                  (455, 415), (455, 425), (515, 425), (455, 425), (515, 425), 
(515, 500), (515, 500), (525, 500)]

walls = [outerWall] 

for wall in walls: 
    for i in range(len(wall) - 1):
        createWall(outerWall[i], outerWall[i+1])    

boxes = [topLeftBox, secondTopLeftBox, topRightBox, secondTopRightBox]

for box in boxes:
    for i in range(len(box)-1):
        createWall(topLeftBox[i], topLeftBox[i+1]),
        createWall(secondTopLeftBox[i], secondTopLeftBox[i+1]),
        createWall(topRightBox[i], topRightBox[i+1]),
        createWall(secondTopRightBox[i], secondTopRightBox[i+1])

Ts = [middleT, leftSidewaysT, rightSidewaysT]

for t in Ts:
    for i in range(len(t)-1):
        createWall(middleT[i], middleT[i+1]),
        createWall(leftSidewaysT[i], leftSidewaysT[i+1]),
        createWall(rightSidewaysT[i], rightSidewaysT[i+1])


horizontalPath = [(40, 610, 9), (60, 290, 14), (60, 50, 12), (60, 170, 33), 
(440, 290, 1),
                       (440, 290, 14), (480, 50, 12), (340, 370, 5), (220, 570, 
18), (580, 610, 8),
                  (0,0,0)]
verticalPath = [(60, 30, 13), (220, 290, 15), (300, 50, 11), (500, 30, 13), 
(340, 290, 4), (340, 290, 4),
                (400, 370, 9), (460, 290, 4), (580, 290, 16), (720, 30, 13), 
(0,0,0)]

paths = [horizontalPath, verticalPath]

for path in paths:
    for i in range(len(path)-1):
        buildPathHorizontal(horizontalPath[i]),
        buildPathVertical(verticalPath[i])


image = 'DustY.gif'
photo = PhotoImage(file=image)
xGif = 100
yGif = 600
ph = canvasOne.create_image(xGif, yGif, image = photo)
#ph

class Images:
    def __init__(self, xGif, yGif, ph):
        self.xGif = xGif
        self.yGif = yGif
        self.ph = ph
        
    def direction(self ):#, event):
        if event.x  <= xGif+10 and event.x >= xGif-10: #+/- tolerance for 
innacurate click
            newY = event.y
            print 'newY = ', newY
            onClickVertical(newY, self.xGif, self.yGif)
        if event.y <= yGif+10 and event.y >= yGif-10: #+/- tolerance for 
innacurate click
            newX = event.x
            print 'newX = ', newX
            onClickHorizontal(newX, self.xGif, self.yGif)

    def onClickVertical(self, newY):
        for i in range(newY):
            yGif += .8
            canvasOne.coords(self.ph, xGif, yGif)
            time.sleep(.001)
            canvasOne.update()

    def onClickHorizontal(self, newX):
        for i in range(newX):
            xGif += .8
            canvasOne.coords(self.ph, xGif, yGif)
            time.sleep(.001)
            canvasOne.update()

        
canvasOne.bind("<Button-1>", Images.direction)

root.mainloop()    

<<attachment: DustY.gif>>

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to