pythontkintermouseeventtkinter-canvas

Moving Images from one canvas to another canvas(es) one by one or randomly without limitation in transferring in tkinter python


Recently I decided to write an application which let user can load bunch of images in a vertical canvas and select desire layout(3x3,2x3,5x2,...) for printing and send each image to blank room of layout and after editing the image quality send the layout to a printer. I could mange to load images in a vertical canvas and send each image to another canvas (not a layout). But I could only send images to another canvas just 2 times and for the third times despite I select an image but it didn't send to another canvas and I feel it hanged for the third times. Here is my script:

import os
from tkinter import *
from PIL import Image, ImageTk


os.system("cls||clear")

def on_click_Canvas1(event):
    global image_id,selected_item,start_x,start_y
    selected_item = canvas1.find_closest(event.x, event.y)[0]
    start_x, start_y = event.x, event.y

def on_click_Canvas2(event):
    global image_id,selected_item,start_x,start_y
    if selected_item:
        x, y = event.x, event.y
        img_tk = image_ids.pop(selected_item, None)
    
        new_id = canvas2.create_image(x, y, image=img_tk)
        image_ids[new_id] = img_tk  
        selected_item = None 
    
    
def on_mousewheel(event):
    canvas1.yview_scroll(int(-1*(event.delta/120)), "units")

root = Tk()

canvas1 = Canvas(root, width=100, height=200, bg="lightblue") 
canvas2 = Canvas(root, width=300, height=200, bg="light pink")
canvas1.pack(side=RIGHT,expand=True,fill=BOTH)
canvas2.pack(side=LEFT,expand=True,fill=BOTH)

selected_item = None
start_x = 0
start_y = 0

image_list = ["E:\images_1\image1.jpg", 
              "E:\images_1\image2.jpg", 
              "E:\images_1\image3.jpg",
              "E:\images_1\image4.jpg", 
              "E:\images_1\image5.jpg", 
              "E:\images_1\image6.jpg"]

i = 0
n= 100
image_append = []
image_ids = {} 

for image in image_list:
    image1 = Image.open(image).resize((n, n))
    tk_image = ImageTk.PhotoImage(image1)
    image_id = canvas1.create_image(100, n*1.1*i, image=tk_image)
    image_append.append(tk_image)
    image_ids[image_id] = tk_image
    i += 1

vsb = Scrollbar(canvas1, orient=VERTICAL,command=canvas1.yview)
vsb.pack(side=RIGHT, fill=Y)
canvas1.config(yscrollcommand=vsb.set, scrollregion=canvas1.bbox(ALL))

# Bind mouse events for dragging
canvas1.bind("<ButtonPress-1>", on_click_Canvas1)
canvas2.bind("<ButtonPress-1>", on_click_Canvas2)

canvas1.bind_all("<MouseWheel>", on_mousewheel)

root.geometry('500x680')
root.mainloop()

I would appreciate if help to let me know the problem issue and remedy as well. Thank you in advance


Solution

  • Moving Images from one canvas to another canvas(es) one by one or randomly without limitation in transferring

    But I could only send images to another canvas just 2 times and for the third times despite I select an image but it didn't send to another canvas and I feel it hanged for the third times.

    Because you use selected_item = None, you are unable to select more than two photos. More than two photos are prevented by that selected_item.

    Change this:

    selected_item = None

    to:

    selected_item += 1

    Screenshot:

    enter image description here

    As you can see, each image is chosen at random or one at a time.