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: -
from Tkinter import *
-
class square:
-
def __init__(self, canvas, xy, color, change):
-
self.canvas = canvas
-
self.id = self.canvas.create_rectangle(-10-abs(change),
-
-10, 11+abs(change), 11, fill = color)
-
self.canvas.move(self.id, xy[0], xy[1])
-
if change > 0:
-
self.change = change
-
self.start = self.right
-
else:
-
self.change = -change
-
self.start = self.left
-
def __call__(self):
-
return self.start
-
def right(self):
-
xy = self.canvas.coords(self.id)
-
if xy[2] >= self.canvas.winfo_width():
-
return self.left()
-
self.canvas.move(self.id, self.change, 0)
-
return self.right
-
def left(self):
-
xy = self.canvas.coords(self.id)
-
if xy[0] <= 0:
-
return sefl.right()
-
self.canvas.move(self.id, -self.delta,0)
-
return self.left
-
-
root = Tk()
-
root.title("square")
-
frame = Frame(root, bd=5, relief=SUNKEN)
-
frame.pack()
-
c=Canvas(frame, width=500, height = 500, bd=0, highlightthickness=0)
-
c.pack()
-
item= [square(c,(10,10), "black", 10)]
-
root.update()
-
try:
-
while 1:
-
for i in range(len(item)):
-
item[i] = item[i]()
-
root.update_idletasks()
-
root.updtate()
-
except TclError:
-
pass
-
-
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
2 17944
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. -
# load and display image using Tkinter
-
from Tkinter import *
-
root = Tk()
-
root.title("Click me!")
-
def next_image(event):
-
global toggle_flag
-
global x, y, photo2, photo3
-
# display photo2, move to right, y stays same
-
canvas1.create_image(x+10, y, image=photo2)
-
canvas1.create_image(x+20, y, image=photo2)
-
canvas1.create_image(x+30, y, image=photo2)
-
canvas1.create_image(x+40, y, image=photo2)
-
canvas1.create_image(x+50, y, image=photo2)
-
canvas1.create_image(x+60, y, image=photo2)
-
canvas1.create_image(x+70, y, image=photo2)
-
canvas1.create_image(x+100, y, image=photo2)
-
-
image1 = "DustY.GIF" #use some random gif
-
photo1 = PhotoImage(file=image1)
-
# make canvas the size of image1/photo1
-
width1 = photo1.width()
-
height1 = photo1.height()
-
canvas1 = Canvas(width=width1, height=height1)
-
canvas1.pack()
-
# display photo1, x, y is center (anchor=CENTER is default)
-
x = (width1)/2.0
-
y = (height1)/2.0
-
canvas1.create_image(x, y, image=photo1)
-
canvas1.bind('<Button-1>', next_image) # bind left mouse click
-
root.mainloop()
-
-
So, the image will go from left to right, but it needs to be deleted after each move. Any ideas?
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.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by Dave Harris |
last post: by
|
4 posts
views
Thread by Christopher Subich |
last post: by
| | | | |
3 posts
views
Thread by skanemupp |
last post: by
|
reply
views
Thread by Guilherme Polo |
last post: by
| | | | | | | | | | | |