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 18202
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |