2

My requirement is i need to drag an image to desired location. Based on the link board-drawing code to move an oval the following is the code snapshot i tried. I am not getting any errors its blank. Please let me know the way to take it forward.

Sample image segment attached

import Tkinter as tk

from Tkinter import *

from PIL import ImageTk, Image

class Example(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        # create a canvas
        self.canvas = tk.Canvas(width=400, height=400)
        self.canvas.pack(fill="both", expand=True)

        # this data is used to keep track of an 
        # item being dragged

        self._drag_data1 = {"x": 0, "y": 0, "item1": None}


    startframe = tk.Frame(root)
    canvas = tk.Canvas(startframe,width=1280,height=720)
    startframe.pack()
    canvas.pack()
    one = tk.PhotoImage(file=r'images/test1.gif')
    root.one = one  # to prevent the image garbage collected.
    canvas.create_image((0,0), image=one, anchor='nw',tags="img1")

    self.canvas.tag_bind("img1", "<1>", self.on_token_press1)
    self.canvas.tag_bind("img1", "<1>", self.on_token_release1)
    self.canvas.tag_bind("token", "<B1-Motion>", self.on_token_motion1)



    def on_token_press1(self, event):
        print("sss")
        # record the item and its location
        self._drag_data1["item1"] = self.canvas.find_closest(event.x, event.y)[0]
        self._drag_data1["x"] = event.x
        self._drag_data1["y"] = event.y

    def on_token_release1(self, event):

        # reset the drag information
        self._drag_data1["item1"] = None
        self._drag_data1["x"] = 0
        self._drag_data1["y"] = 0

    def on_token_motion1(self, event):
        '''Handle dragging of an object'''
        # compute how much the mouse has moved
        delta_x = event.x - self._drag_data1["x"]
        delta_y = event.y - self._drag_data1["y"]
        # move the object the appropriate amount
        self.canvas.move(self._drag_data1["item1"], delta_x, delta_y)
        # record the new position
        self._drag_data1["x"] = event.x
        self._drag_data1["y"] = event.y
if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
Community
  • 1
  • 1
Asha Datla
  • 126
  • 1
  • 11
  • Are the statements in `def __init__` misaligned on purpose? For that for sure would break the code.. Or is it simply a matter of a copy/paste/markup mistake in your question? – Montmons Apr 04 '17 at 10:26
  • Btw, there appear multiple misalignments all over your code snippet. Please double check if these are markup mistakes or not.. indentation is important in Python. – Montmons Apr 04 '17 at 10:29
  • Based on few updations changed the code – Asha Datla Apr 05 '17 at 14:41
  • `root` is not defined inside the class, so it gives an error in line: `startframe = tk.Frame(root)` NameError: name 'root' is not defined –  Apr 05 '17 at 15:00
  • See my answer, did it work? – Montmons Apr 10 '17 at 16:16

1 Answers1

0

As said in the comments:

Indentation is important!

Within the Example-class its initiation method this entire code-block is misaligned:

startframe = tk.Frame(root)
canvas = tk.Canvas(startframe,width=1280,height=720)
startframe.pack()
canvas.pack()
one = tk.PhotoImage(file=r'images/test1.gif')
root.one = one  # to prevent the image garbage collected.
canvas.create_image((0,0), image=one, anchor='nw',tags="img1")

self.canvas.tag_bind("img1", "<1>", self.on_token_press1)
self.canvas.tag_bind("img1", "<1>", self.on_token_release1)
self.canvas.tag_bind("token", "<B1-Motion>", self.on_token_motion1)

If I simply add a Tab to the indentation of the entire block I was able to run the OP's code without any problem.

Since the script requires a GIF file to be placed in images/test1.gif I downloaded and used this GIF: enter image description here

tkinter doesn't seem to actually play the gif (which isn't asked by the OP), but it does indeed show it.

Montmons
  • 1,416
  • 13
  • 44
  • thank you, how do i move the selected image to another canvas ? – Asha Datla Apr 11 '17 at 18:51
  • @AshaDatla - Ah, well that was your question of course.. my mistake. Well, I won't go through the trouble of correcting your code as it would take up a bit too much of my time right now. However, there are 2 things you should really realize: (1) Read up on Python Classes vs. Definitions as your code seems a bit conflicted on the use of `self.` (2), notice that the original source of your code is over 5 years old and way outdated. For a simpler, working example of moving an image see [this](http://stackoverflow.com/a/23275578/4041795) answer. See if you can work your way up from there... – Montmons Apr 11 '17 at 20:02