Line acts strange when hovered over Button

  button, python, tkinter, user-interface

I have a visual problem in my Python Tkinter GUI:

I have an overall canvas connected to my root window:
window_canvas = Canvas(root, borderwidth=0, background="grey", width = 500, height = 500, highlightthickness=0)

I placed a smaller frame in this canvas to place buttons:
window_frame = Frame(window_canvas, background='white', borderwidth=0, width = canvas_size[0], height = canvas_size[1])

and a canvas again to place forms like rectangles, circles etc. with the same dimensions as window_frame:
canvas = Canvas(window_frame, borderwidth=0, background="white", width = canvas_size[0], height = canvas_size[1], highlightthickness=0)

Sounds strange maybe. But I need all those things to create a valid GUI for my application.

Now, I created a line with canvas.create_line. One of its ends follows the mouse cursor. I get the according coordinates from:

new_x = canvas.canvasx(event.x)
new_y = canvas.canvasy(event.y)

I also placed a button within the window_frame at certain coordinates:

button1 = Button(window_frame, text='    ', relief=SOLID, borderwidth=1, highlightbackground='#9c9c9c', bg='white')
button1.place(x=100, y=100)

The strange thing I’m experiencing right now is, that when my mouse cursor hovers over the button, the line is suddenly not following the exact mouse coordinates anymore but has a major offset to the left top of the canvas. It seems that the area of the button is affecting the event coordinates. When my cursor leaves the button, the line jumps back to the cursor again.
Has someone a solution/explanation to that?

Regards!

Source: Python Questions

LEAVE A COMMENT