鼠标按键在Tkinter中忽略事件?

时间:2019-03-25 19:28:03

标签: python python-3.x tkinter mouseevent

以下代码段应报告鼠标单击事件,但从未报告过Bx-Up事件...这是错误还是我错过了某些事情?

#!/usr/bin/python3
# coding: UTF-8

# Testing mouse events in tkinter

import tkinter as tk

class MyMouse:

    def __init__(self):

        def init_vars():

            self.padx, self.pady = 40, 40
            self.xsize, self.ysize = 800, 600

        def create_window():

            def create_canvas(wdth, hght):
                canvas = tk.Canvas(mywin, width=wdth, height=hght, bg='white')
                canvas.bind(sequence='<1>', func=self.b1)
                canvas.bind(sequence='<B1-Motion>', func=self.b1_motion)
                canvas.bind(sequence='<B1-Up>', func=self.b1_up)
                canvas.bind(sequence='<2>', func=self.b2)
                canvas.bind(sequence='<B2-Motion>', func=self.b2_motion)
                canvas.bind(sequence='<B2-Up>', func=self.b2_up)
                canvas.bind(sequence='<3>', func=self.b3)
                canvas.bind(sequence='<B3-Motion>', func=self.b3_motion)
                canvas.bind(sequence='<B3-Up>', func=self.b3_up)
                return canvas

            mywin = tk.Tk()
            mywin.title("Testing mouse events in tkinter")
            mywin.canvas = create_canvas(self.xsize, self.ysize)
            mywin.canvas.pack(side=tk.TOP, padx=self.padx, pady=self.pady,
                                expand=True, fill=tk.BOTH)
            return mywin

        init_vars()
        self.mywin = create_window()
        self.mywin.mainloop()


    def b1(self, event):
        print('b1 called', (event.x, event.y), flush=True)

    def b1_motion(self, event):
        print('b1_motion called', (event.x, event.y), flush=True)

    def b1_up(self, event):
        print('b1_up called', (event.x, event.y), flush=True)

    def b2(self, event):
        print('b2 called', (event.x, event.y), flush=True)

    def b2_motion(self, event):
        print('b2_motion called', (event.x, event.y), flush=True)

    def b2_up(self, event):
        print('b2_up called', (event.x, event.y), flush=True)

    def b3(self, event):
        print('b3 called', (event.x, event.y), flush=True)

    def b3_motion(self, event):
        print('b3_motion called', (event.x, event.y), flush=True)

    def b3_up(self, event):
        print('b3_up called', (event.x, event.y), flush=True)

MyMouse()

报告:

b1 called (452, 238)
b1 called (452, 238)
b1 called (452, 238)
b1 called (452, 238)
b2 called (452, 238)
b2 called (452, 238)
b3 called (452, 238)
b3 called (452, 238)
b3 called (452, 238)
b1 called (381, 242)
b1_motion called (381, 243)
b1_motion called (399, 255)
b1_motion called (410, 262)
b1_motion called (413, 263)
b2 called (415, 264)

评论:我相信前面的问题在很大程度上是自给自足的,但是该程序不允许我添加更多评论。因此,请提出问题,以便我可以根据需要添加更多评论。这篇文章是关于事件管理器的,从未被称为...

1 个答案:

答案 0 :(得分:2)

我没有在文档中看到B1-Uphttp://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

不过,我确实看到了ButtonRelease-1。这是要绑定的正确事件。