Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:40:14+00:00 2026-06-15T14:40:14+00:00

My code is supposed to create multiple images in the canvas. And I should

  • 0

My code is supposed to create multiple images in the canvas. And I should be able to join them with lines. The images are drag and drop enabled. I suppose I need to handle multiple events in my canvas. Everytime I want to draw a line between two, the image gets dragged too. I dont know how to exclusively call one event to my canvas
Pease help and sorry for the crappy code.

from Tkinter import *
import tkMessageBox


def callback():
        if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
            root.destroy()

class AppUI(Frame):
        def __init__(self, master):

            Frame.__init__(self, master, background="white", relief=FLAT, bd=2, height=768, width=1024)

            self.menubar = Menu(self)

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="File", menu=menu)
            menu.add_command(label="New")
            menu.add_command(label="Open")
            menu.add_command(label="Save As")
            menu.add_command(label="Save Copy of")
            menu.add_separator()
            menu.add_command(label="exit")

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="Edit", menu=menu)
            menu.add_command(label="Cut")
            menu.add_command(label="Copy")
            menu.add_command(label="Paste")

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="Debug", menu=menu)
            menu.add_command(label="Open Debugger")
            menu.add_command(label="Auto-debug")
            menu.add_command(label="Revert")

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="Run", menu=menu)
            menu.add_command(label="Stimulation")
            menu.add_command(label="Check")
            menu.add_command(label="Scan Module")

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="Help", menu=menu)
            menu.add_command(label="Help files")
            menu.add_command(label="FTA site")
            menu.add_separator()
            menu.add_command(label="Credits")

            self.master.config(menu=self.menubar)      

            f0 = Frame(root, background="white")

            b1 = Button(f0, bd=2, padx=15, pady=15, justify = LEFT)
            photo1 = PhotoImage(file="Images/p1.gif")
            b1.config(image = photo1,width="50",height="50", command=self.create_image1)
            b1.image = photo1
            b1.pack(side=LEFT)

            b2 = Button(f0, bd=2, padx=15, pady=15, justify = LEFT)
            photo2 = PhotoImage(file="Images/p4.gif")
            b2.config(image=photo2, width="50",height="50", command = self.create_image2)
            b2.image = photo2
            b2.pack(side=LEFT)

            b3 = Button(f0,padx=15, bd=2, pady=15, justify = LEFT)
            photo3 = PhotoImage(file="Images/p8.gif")
            b3.config(image=photo3, width="50",height="50", command = self.create_image3)
            b3.image = photo3
            b3.pack(side=LEFT)

            b4 = Button(f0,padx=15, bd=2, pady=15, justify = LEFT)
            photo4 = PhotoImage(file="Images/p7.gif")
            b4.config(image=photo4, width="50",height="50", command = self.create_image4)
            b4.image = photo4
            b4.pack(side=LEFT)

            b5 = Button(f0,padx=15, bd=2, pady=15, justify = LEFT)
            photo5 = PhotoImage(file="Images/p5.gif")
            b5.config(image=photo5, width="50",height="50", command = self.printtheline)
            b5.image = photo5
            b5.pack(side=LEFT)

            f0.pack(anchor=NW, side=TOP)

            self.imglist = []

            self.canvas = Canvas(self, height=750, width=1500, bg="white")
            self.canvas.pack(side=LEFT)

    def create_image1(self):
            photos1 = PhotoImage(file="Images/p1.gif")
            self.photos1=photos1
            self.imglist.append(photos1)
            self.img1=self.canvas.create_image(60, 60, image=photos1)
            self.canvas.bind("<B1-Motion>", self.move_image1)
            self.canvas.bind("<ButtonRelease-1>")

    def create_image2(self):
            photos2 = PhotoImage(file="Images/p4.gif")
            self.photos2=photos2
            self.imglist.append(photos2)
            self.img2=self.canvas.create_image(60, 60, image=photos2)
            self.canvas.bind("<B1-Motion>", self.move_image2)

    def create_image3(self):
            photos3 = PhotoImage(file="Images/p8.gif")
            self.photos3=photos3
            self.imglist.append(photos3)
            self.img3=self.canvas.create_image(60, 60, image=photos3)
            self.canvas.bind("<B1-Motion>", self.move_image3)

    def create_image4(self):
            photos4 = PhotoImage(file="Images/p7.gif")
            self.photos4=photos4
            self.imglist.append(photos4)
            self.img4=self.canvas.create_image(60, 60, image=photos4)
            self.canvas.bind("<B1-Motion>", self.move_image4)


    def move_image1(self, event):
            self.canvas.delete(self.img1)
            x = event.x
            y = event.y
            self.img1 = self.canvas.create_image(x, y, image=self.photos1, anchor='nw')
            self.canvas.update()
    def move_image2(self, event):
            self.canvas.delete(self.img2)
            x = event.x
            y = event.y
            self.img2 = self.canvas.create_image(x, y, image=self.photos2, anchor='nw')
            self.canvas.update()
    def move_image3(self, event):
            self.canvas.delete(self.img3)
            x = event.x
            y = event.y
            self.img3 = self.canvas.create_image(x, y, image=self.photos3, anchor='nw')
            self.canvas.update()
    def move_image4(self, event):
            self.canvas.delete(self.img4)
            x = event.x
            y = event.y
            self.img4 = self.canvas.create_image(x, y, image=self.photos4, anchor='nw')
            self.canvas.update()

    def getevent(self, event):
            self.x = event.x
            self.y = event.y

    def createline(self, event):
            x0,y0 = (self.x, self.y)
            x1,y1 = (event.x, event.y)
            self.canvas.create_line(x0,y0,x1,y1, fill="black")

    def printtheline(self):
            self.canvas.bind("<Button-1>", self.getevent)
            self.canvas.bind("<ButtonRelease-1>", self.createline)

root = Tk()
root.protocol("WM_DELETE_WINDOW", callback)

app = AppUI(root)
app.pack()

root.mainloop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:40:15+00:00Added an answer on June 15, 2026 at 2:40 pm

    Every time you call self.canvas.bind("<B1-Motion>", ...) you are destroying any old binding you have and creating a new one. Thus, the last time that you call it is the one that fires every time. You also definitely don’t want to be calling update from within a function called by a binding.

    What you need to do is create a three distinct bindings on the mouse button. On a button press, you need to figure out which object is under the cursor and save a reference to it, as well as the starting x,y. On button motion, you use that saved reference and call canvas_move using the current x,y to compute a delta from the previous time it was moved. Finally, on a mouse down you can do any finishing up that needs to happen.

    For a fully working example see this answer to the question board-drawing code to move an oval.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table like: create table { id numeric(5,3), code varchar(10) }
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
This code is supposed to be able to sort the items in self.array based
The following code is supposed to create a new (modified) version of a frequency
I've created this code, it is supposed to use wordpress metabox functionality to save
This code is supposed to read postfix problems from a file and and write
The following code is supposed to find electors that do not have a postal
This piece of code is supposed to go through a list and preform some
The following code is supposed to make the right 60% of the display red.
The code is for a view debate page. The code is supposed to determine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.