Python/tkinter/canvas:mousedown+drag+mouseup事件是否只返回mousedown项标记?

2024-09-30 16:27:50 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python/tkinter/canvas上:用户将单击一个项目,拖动到另一个项目并释放。项目本身不会被拖动/移动;我只需要这两个项目的标签。我观察到的问题是,当鼠标落在一个项目上,然后拖动到另一个项目以释放鼠标时,画布返回第一个项目id#/tag作为两个事件id#/tags

鼠标坐标可以正确地报告这两个事件,标记在其他方面也可以正常工作

这是一个bug还是实现限制?有什么解决办法吗?还是我做错了

下面的演示代码也位于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()

Tags: thetextposselfmastereventtagplace
1条回答
网友
1楼 · 发布于 2024-09-30 16:27:50

这不是一个bug,也不是一个真正的限制。这就是tkinter的设计初衷。获取按钮按下事件的小部件和/或画布项将接收后续发布事件

您需要让绑定到发布事件的代码找到光标下的画布项

例如,给定一个事件对象,下面是如何查找距离指针最近的画布项:

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

相关问题 更多 >