473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

TMS
119 100+
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
2 18202
TMS
119 100+
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
6,596 Expert 4TB
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

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

Similar topics

1
by: Dave Harris | last post by:
I derived two Frame classes and created a Canvas in each. I was extremely surprised when frame1.canvas.delete('all') erased the image in frame2.canvas! I resolved my trouble by assigning tags...
4
by: Christopher Subich | last post by:
I'm building an application involving both twisted and Tkinter. Since twisted co-opts <widget>.mainloop() in its reactor.run(), and since it behaves very badly if the application quits without...
4
TMS
by: TMS | last post by:
Is there any way of determining the location of a gif grom an arrow event? For example I have a canvas that has a gif that moves and I want the arrow to move the gif. But how does the arrow know...
1
by: rampm2007 | last post by:
Hi, I am using the XmlTextReader along XmlValidatingReader for parsing the XML file. Becuase of the foreign characters sometimes I get the "There is an invalid character in the given encoding"...
1
by: dolittle | last post by:
Hi, I`m investigating the <canvas> element which is implemented in FF and it`s implementation in IE using explorercanvas http://code.google.com/p/explorercanvas/ How can I find out what is the...
0
by: aboobackerpm | last post by:
i am using a binded dgv with datatable my problem is how i can update the changes in a cell of a dgv to datatable without move to next cell my dgv's allow addnew property is false because of...
3
by: skanemupp | last post by:
so i load a gif onto a canvas and when i click the canvs i want to get the color of the pixel that is clicked. so i need to ge the object im clicking. i was told in another thread to use...
0
by: Guilherme Polo | last post by:
On 10/29/08, Olrik Lenstra <o.lenstra@gmail.comwrote: It will be a combination of commands, not a single one. Initially I considered this as "probably without solution", since tcl acquired a...
7
by: Haiyan | last post by:
Dear experts: I am trying to add one xy-scrollbar on the canvas, then put lots of check buttons on the canvas. I got at least following 2 problems: 1.I can see the y-scroll bar but contents on...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.