tkinter objects ID created with create_window not accessible on canvas

Viewed 52

I am stack with an issue for some time now.

I am creating a class with instances being a text widgets movable on a canvas. To my understanding I need for that a canvas.create_window() widget.

I manage to create the instance of the class and place it on the canvas. The object is build as a "frame_xy" in which I place a text field. The final element (?) is then created by using

self.feature_id = self.canvas.create_window(x, y window= self.frame_xy, tags= 'xyz') 

method. Although I have the tag and obviously I have an id of the object (self.feature_id) I cannot detect the element when I click on it with the mouse. However the canvas.find_closest(event.x, event.y) in the main body of the code detects the element, when I place the mouse close to it!

When I try the same with the standard elements of canvas (e.g. rectangle, oval, and text ) everything works fine.

Is there anything special with the canvas.create_window()? I would appreciate any help of where to find some more info about this!

Thanks a lot!


from tkinter import Tk, Frame, Canvas, Text, CURRENT, END, TRUE

class features_object():
    
    def __init__(self, master, feature_text, feature_xpos, feature_ypos, feature_color):
    
        self.master = master
        self.feature_text = feature_text
        self.feature_xpos = feature_xpos
        self.feature_ypos = feature_ypos
        self.feature_color = feature_color
        self.create_feature()

    def featureMove(self, event):

        self.lastx = event.x
        self.lasty = event.y
        self.master.move(CURRENT, event.x - self.lastx, event.y - self.lasty)

    def create_feature(self):

        # self.feature = self.master.create_text(50, 50, text=self.feature_text, fill=self.feature_color, font=('Helvetica 15 bold'), tags='feature')
        
        self.feature_frame = Frame(self.master, bg='red', border=8)      # Make the feature color the same as the color of the claim it commes from     
        self.feature_frame.pack()

        # The text box for the features#

        self.feature_text = Text(self.feature_frame, font=("Helvetica", 12), relief='flat', width=20, height=3, selectbackground=self.feature_color, selectforeground="black", exportselection=TRUE)
        self.feature_text.grid(row=0, column=0, padx=5, pady=5, sticky='w')
        self.feature_text.config(wrap='word')
        self.feature_text.insert(END, "XY")
        self.feature_id = self.master.create_window(self.feature_xpos, self.feature_ypos, window=self.feature_frame, tags='feature')

        self.feature_frame.bind('<B1-Motion>', self.featureMove)


def read_id(event):
    currently_clicked = draw_canvas.find_closest(event.x, event.y)
    print(f'Currently clicked instance {currently_clicked}')


window = Tk()

frame_start = Frame(window)
frame_start.pack()

draw_canvas = Canvas(frame_start)
draw_canvas.pack()

created_feature_1 = features_object(draw_canvas, 'feature A', 100, 100, 'red')

draw_canvas.bind('<Button-3>', read_id)

if __name__ == '__main__':
    window.mainloop()
0 Answers
Related