Python / tkinter / canvas: события mouse-down + drag + mouse-up возвращают только тег элемента click-down? - PullRequest
0 голосов
/ 01 марта 2020

На Python / tkinter / canvas: пользователь нажимает на один элемент, перетаскивает на другой элемент и отпускает. Сами предметы не перетаскиваются / перемещаются; Мне нужны только теги от обоих предметов. Проблема, которую я наблюдаю, состоит в том, что когда нажатие мыши находится на одном элементе, а затем перетаскивается на другой элемент для отпускания мыши, холст возвращает идентификатор первого элемента # / tag как оба идентификатора события # / tag.

Координаты мыши сообщают правильно для обоих событий, а теги работают правильно во всех остальных отношениях.

Это ошибка или ограничение реализации? Есть ли обходные пути? Или я делаю это неправильно?

Демонстрационный код ниже также на https://pastebin.com/MD9AuAUS

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from functools import partial


'''
Ctag Tester v1.0
29 February 2020
Written for Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) 
[MSC v.1914 32 bit (Intel)] on win32 / Windows 10 / VS Code (ide)

This is a demo of a tkinter / canvas bug. Or something. (?)

1. Each of the two squares on the canvas should report its tag 
for a mouse-1-down and mouse-1-up event.

2. It always reports properly if you click and release on the same item.

3. PROBLEM: If you click down on one square and then drag to the other
square, the mouse-up event gives the same tag as the mouse-down event,
even though they did not occur on the same item.

4. The mouse x-y coordinates are also reported, and those always
report correctly wherever those two events occur.

5. What I assumed should happen is that the mouse-down event should get
the tag of the item that it occurs on, and the mouse-up event should get
the tag of the item that it occurs on.
'''


def main():
    program = TestProgram1()
    program.window.mainloop()

class TestProgram1:

    def __init__(self):
        self.window = tk.Tk()
        self.window.title( "Ctag Tester v1.0" )
        self.window_width = 800
        self.window_height = 600
        self.window.minsize(self.window_width, self.window_height)
        self.window_position_x = 40
        self.window_position_y = 40
        self.window.geometry('%dx%d+%d+%d' % (self.window_width, self.window_height, self.window_position_x, self.window_position_y))
        self.window.resizable(False, False)



        x_pos = 20
        y_pos = 20
        self.staticlabel_38 = tk.Label(master=self.window, text="mousedown xy:")
        self.staticlabel_38.place(x=x_pos, y=y_pos)
        x_pos = 180
        y_pos = 20
        self.staticlabel_42 = tk.Label(master=self.window, text="mouseup xy:")
        self.staticlabel_42.place(x=x_pos, y=y_pos)

        x_pos = 20
        y_pos = 50
        self.label_mousedown = tk.Label(master=self.window, text="[none]")
        self.label_mousedown.place(x=x_pos, y=y_pos)
        x_pos = 180
        y_pos = 50
        self.label_mouseup = tk.Label(master=self.window, text="[none]")
        self.label_mouseup.place(x=x_pos, y=y_pos)

        x_pos = 20
        y_pos = 100
        self.staticlabel_55 = tk.Label(master=self.window, text="mousedown tag:")
        self.staticlabel_55.place(x=x_pos, y=y_pos)
        x_pos = 180
        y_pos = 100
        self.staticlabel_59 = tk.Label(master=self.window, text="mouseup tag:")
        self.staticlabel_59.place(x=x_pos, y=y_pos)

        x_pos = 20
        y_pos = 130
        self.label_tagdown = tk.Label(master=self.window, text="[none]")
        self.label_tagdown.place(x=x_pos, y=y_pos)
        x_pos = 180
        y_pos = 130
        self.label_tagup = tk.Label(master=self.window, text="[none]")
        self.label_tagup.place(x=x_pos, y=y_pos)

        x_pos = 350
        y_pos = 40
        self.canvas1 = tk.Canvas(master=self.window, width=400, height=400)
        self.canvas1.place(x=x_pos, y=y_pos)
        self.canvas1.create_rectangle(2, 2, 400, 400, outline='black', fill='white')

        target_1 = self.canvas1.create_rectangle(80, 80, 180, 180, outline='black', fill='green', tags=('greenbox'))
        self.canvas1.tag_bind('greenbox', '<Button-1>', partial(self.chart_item_mousedown_event, target_1))
        self.canvas1.tag_bind('greenbox', '<ButtonRelease-1>', partial(self.chart_item_mouseup_event, target_1))      

        target_2 = self.canvas1.create_rectangle(240, 180, 340, 280, outline='black', fill='red', tags=('redbox'))
        self.canvas1.tag_bind('redbox', '<Button-1>', partial(self.chart_item_mousedown_event, target_2))
        self.canvas1.tag_bind('redbox', '<ButtonRelease-1>', partial(self.chart_item_mouseup_event, target_2))



    def chart_item_mousedown_event(self, c_handle, event):
        temp_tag = self.canvas1.gettags(c_handle)
        if temp_tag == "":
            self.label_tagdown.config(text='[none]')
        else:
            self.label_tagdown.config(text=temp_tag)
        m_event = "x:" + str(event.x) + ", y:" + str(event.y)
        self.label_mousedown.config(text=m_event)



    def chart_item_mouseup_event(self, c_handle, event):
        temp_tag = self.canvas1.gettags(c_handle)
        if temp_tag == "":
            self.label_tagup.config(text='[none]')
        else:
            self.label_tagup.config(text=temp_tag)
        m_event = "x:" + str(event.x) + ", y:" + str(event.y)
        self.label_mouseup.config(text=m_event)



if __name__ == "__main__":
    main()

1 Ответ

0 голосов
/ 01 марта 2020

Это не ошибка и не является ограничением. Это просто, как tkinter был разработан для работы. Виджет и / или элемент холста, который получает событие нажатия кнопки, - это тот, который получит последующее событие релиза.

Вам потребуется код, связанный с событием релиза, для поиска элемента холста, который под курсором.

Например, с учетом объекта события, вот как найти элемент холста, ближайший к указателю:

canvas = event.widget
canvasx = canvas.canvasx(event.x)
canvasy = canvas.canvasy(event.y)
item = canvas.find_closest(canvasx, canvasy)
...