Rinrin
Rinrin

Reputation: 33

Python Tkinter canvas.tag_bind()

I've been writing this code on Python using Tkinter also, that at the end of the day, i'll be able to click on a picture ( and not on a specific dot with a x,y in the picture's borders, but anywhere in the entire picture's borders ) and then click anywhere on the screen, and be able to move it there.

My code:

from Tkinter import *
from PIL import ImageTk, Image

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#Constants~
CLICKED = False

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#Classes and Functions~
class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()

def asign_click(event):
    CLICKED = True
    print CLICKED
def new_destination(event, x, y, canvas, picture):
    if CLICKED == True:
        canvas.move(picture, x, y)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#Main~
myapp = App()
myapp.master.title("DA-I-SUKI!! <3~")
myapp.master.maxsize(2000, 1200)

my_picture = ImageTk.PhotoImage(Image.open("SOME_PICTURE"))
canvas = Canvas(height = my_picture.height(), width = my_picture.width())
canvas.pack(expand=1, fill=BOTH)
picture_pack = canvas.create_image(1130, 500, image = my_picture)

canvas.tag_bind(my_picture, '<1>', asign_click) 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
myapp.mainloop()

Basically, I want the tag_bind at the end of the code to work so it can call asign_click(), and change CLICKED's constant value to TRUE, and then add another tag_bind ( or anything else to work ) that'll allow me to click anywhere on the screen and call new_destination() so it'll move the picture to its new destination.

Thanks a lot in advance.

Upvotes: 1

Views: 4081

Answers (1)

VRage
VRage

Reputation: 1498

You just have a litte mistake in tag_unbind, according to effbot.org this method needs a tag or an id. But what you pass is a image object. Whenever you call create_... from a canvas object an id is given on return.

So just change your canvas.tag_bind(...) line to canvas.tag_bind(picture_pack, '<1>', asign_click)

Upvotes: 1

Related Questions