lokesh
lokesh

Reputation: 330

how to select multiple objects with mouse in tkinter python gui?

I am trying to select multiple objects using mouse just like in windows click and drag. i am using tkinter in python to buils this gui. i am creating objects as shown in below code.

import Tkinter as tk
from Tkinter import *

root = Tk()
w= Canvas(root, width=800, height=768)
w.grid()
w.create_line(200,200,300,300, width=3, tags="line1")
w.create_oval(150,100,170,300, fill="red", tags="oval")

mainloop()

what i am trying to do is if i drag my mouse over multiple objects some def should return the tags of the objects. how can i do this.

Thank you

Upvotes: 3

Views: 3222

Answers (2)

cweb
cweb

Reputation: 430

The following code renders a rectangle that follows the cursor and returns a list of element IDs of elements within the rectangle. It uses bindings for when the mouse is pressed within the canvas, when it moves, and when it is released. I then kept a list of elements with their canvas identifier and used that to back lookup the object from the list of selected IDs.

# used to record where dragging from
originx,originy = 0,0

canvas.bind("<ButtonPress-1>", __SelectStart__)
canvas.bind("<B1-Motion>", __SelectMotion__)
canvas.bind("<ButtonRelease-1>", __SelectRelease__)

# binding for drag select
def __SelectStart__(self, event):
    oiginx = canvas.canvasx(event.x)
    originy = canvas.canvasy(event.y)
    selectBox = canvas.create_rectangle(originx,originy,\
        originx,originy)

# binding for drag select
def __SelectMotion__(self, event):
    xnew = canvas.canvasx(event.x)
    ynew = canvas.canvasy(event.y)
    # correct cordinates so it gives (upper left, lower right)
    if xnew < x and ynew < y:
        canvas.coords(selectBox,xnew,ynew,originx,originy)
    elif xnew < x:
        canvas.coords(selectBox,xnew,originy,originx,ynew)
    elif ynew < y:
        canvas.coords(selectBox,originx,ynew,xnew,originy)
    else:
        canvas.coords(selectBox,originx,originy,xnew,ynew)

# binding for drag select
def __SelectRelease__(self, event):
    x1,y1,x2,y2 = canvas.coords(selectBox)
    canvas.delete(selectBox)
    # find all objects within select box
    selectedPointers = []
    for i in canvas.find_withtag("tag"):
        x3,y3,x4,y4 = canvas.coords(i)
        if x3>x1 and x4<x2 and y3>y1 and y4<y2:
            selectedPointers.append(i)
    Callback(selectedPointers)

# function to receive IDs of selected items
def Callback(pointers):
    print(pointers)

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386230

Save the coordinates on a button-down event, and then on a button-up event use the find_enclosed or find_overlapping method of the canvas to find all items enclosed by the region.

Upvotes: 2

Related Questions