By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Tkinter -> canvas -> make an obect move from one spot to next

TMS
100+
P: 119
Schools over!!! Now its time to play.

I would like to learn how to make objects move from one location to the next on a canvas widget. For example:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. class square:
  3.     def __init__(self, canvas, xy, color, change):
  4.         self.canvas = canvas
  5.         self.id = self.canvas.create_rectangle(-10-abs(change),
  6.                                              -10, 11+abs(change), 11, fill = color)
  7.         self.canvas.move(self.id, xy[0], xy[1])
  8.         if change > 0:
  9.             self.change = change
  10.             self.start = self.right
  11.         else:
  12.             self.change = -change
  13.             self.start = self.left
  14.     def __call__(self):
  15.         return self.start
  16.     def right(self):
  17.         xy = self.canvas.coords(self.id)
  18.         if xy[2] >= self.canvas.winfo_width():
  19.             return self.left()
  20.         self.canvas.move(self.id, self.change, 0)
  21.         return self.right
  22.     def left(self):
  23.         xy = self.canvas.coords(self.id)
  24.         if xy[0] <= 0:
  25.             return sefl.right()
  26.         self.canvas.move(self.id, -self.delta,0)
  27.         return self.left
  28.  
  29. root = Tk()
  30. root.title("square")
  31. frame = Frame(root, bd=5, relief=SUNKEN)
  32. frame.pack()
  33. c=Canvas(frame, width=500, height = 500, bd=0, highlightthickness=0)
  34. c.pack()
  35. item= [square(c,(10,10), "black", 10)]
  36. root.update()
  37. try:
  38.     while 1:
  39.         for i in range(len(item)):
  40.             item[i] = item[i]()
  41.             root.update_idletasks()
  42.         root.updtate()
  43. except TclError:
  44.     pass
  45.  
  46.  
I would like the box to move from one spot to the next. I have been trying to modify a program that I found that moves 'blobs' back and forth. What I want to (eventually) do is move the square from one location to another based on a mouse event.So, I figure the first thing I really need to do is learn how to move the box from one 'hard coded' location to another 'hard coded location.' Can't seem to get this part, so I tried just moving the object like the 'blobs' program. But that isn't working either.

Any advice, tips, hints, whatever would be greatly appreciated.

tms
May 3 '07 #1
Share this Question
Share on Google+
2 Replies


TMS
100+
P: 119
TMS
OK, better yet... I have a gif moving now, but I want to make the image delete after it moves a little bit. I suppose I should have a loop doing what I'm doing by hand, but .... I'm just trying to get the thing to work.

Expand|Select|Wrap|Line Numbers
  1. # load and display image using Tkinter
  2. from Tkinter import *
  3. root = Tk()
  4. root.title("Click me!")
  5. def next_image(event):
  6.     global toggle_flag
  7.     global x, y, photo2, photo3
  8.     # display photo2, move to right, y stays same
  9.     canvas1.create_image(x+10, y, image=photo2)
  10.     canvas1.create_image(x+20, y, image=photo2)           
  11.     canvas1.create_image(x+30, y, image=photo2)
  12.     canvas1.create_image(x+40, y, image=photo2)
  13.     canvas1.create_image(x+50, y, image=photo2)
  14.     canvas1.create_image(x+60, y, image=photo2)
  15.     canvas1.create_image(x+70, y, image=photo2)
  16.     canvas1.create_image(x+100, y, image=photo2)
  17.  
  18. image1 = "DustY.GIF"   #use some random gif
  19. photo1 = PhotoImage(file=image1)
  20. # make canvas the size of image1/photo1
  21. width1 = photo1.width()
  22. height1 = photo1.height()
  23. canvas1 = Canvas(width=width1, height=height1)
  24. canvas1.pack()
  25. # display photo1, x, y is center (anchor=CENTER is default)
  26. x = (width1)/2.0
  27. y = (height1)/2.0
  28. canvas1.create_image(x, y, image=photo1)
  29. canvas1.bind('<Button-1>', next_image)  # bind left mouse click
  30. root.mainloop() 
  31.  
  32.  
So, the image will go from left to right, but it needs to be deleted after each move. Any ideas?
May 3 '07 #2

bartonc
Expert 5K+
P: 6,596
According to this code by kudos, it's the coords() function that moves an object. He also posted and Easter graphic, but I haven't run it.
May 4 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.