Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单击、双击并移动鼠标_Python_Tkinter - Fatal编程技术网

Python 单击、双击并移动鼠标

Python 单击、双击并移动鼠标,python,tkinter,Python,Tkinter,我有一个使用tkinter的应用程序,其中鼠标点击、双击和移动鼠标时需要执行不同的操作。下面的课程很有效,但是我想知道是否没有更简单的或者tkinter内置的方法来实现这一点 from tkinter import Tk, Canvas class MouseControl: ''' Class for mouse control to establish if there is a 'click', 'double click' or mouse is bein

我有一个使用tkinter的应用程序,其中鼠标点击、双击和移动鼠标时需要执行不同的操作。下面的课程很有效,但是我想知道是否没有更简单的或者tkinter内置的方法来实现这一点

from tkinter import Tk, Canvas


class MouseControl:
    '''  Class for mouse control to establish if there is a 'click',
         'double click' or mouse is being moved '''
    def __init__(self, aw):
        self.double_click_flag = False
        self.button_released_flag = False
        self.aw = aw
        self.aw.bind('<Button-1>', self.clicked)  # bind left mouse click
        self.aw.bind('<Double-1>', self.double_click)  # bind double left clicks
        self.aw.bind('<ButtonRelease-1>', self.button_released)  # bind button release
        self.aw.bind('<B1-Motion>', self.moved)  # bring when mouse is moved

    def clicked(self, event):
        '''  add a little delay before calling action to allow for double click
         and button released to have occurred '''
        self.double_click_flag, self.button_released_flag = False, False
        self.aw.after(300, self.action, event)

    def double_click(self, event):
        '''  set flag when there is a double click '''
        self.double_click_flag = True

    def button_released(self, event):
        '''  set flag when button is released '''
        self.button_released_flag = True

    def moved(self, event):
        '''  define action on when mouse is moved in this case just printing
             the coordinates'''
        print('mouse position is at ({:03}. {:03})'.
              format(event.x, event.y), end='\r')

    def action(self, event):
        '''  define action on click and double click in this case just printing
             the event '''
        if self.button_released_flag:

            if self.double_click_flag:
                print('double mouse click event')

            else:
                print('single mouse click event')


root = Tk()
window = Canvas(root, width=400, height=400, bg='grey')
mouse = MouseControl(window)
window.place(x=0, y=0)
window.mainloop()
从tkinter导入Tk,画布
类鼠标控件:
''类,用于确定是否存在“单击”,
“双击”或正在移动鼠标“”
定义初始(自我,aw):
self.double\u click\u标志=False
self.button_released_标志=False
self.aw=aw
self.aw.bind(“”,self.clicked)#绑定鼠标左键单击
self.aw.bind(“”,self.双击)#绑定双击
self.aw.bind(“”,self.button_release)#绑定按钮释放
self.aw.bind(“”,self.moved)#移动鼠标时带来
已单击的定义(自身、事件):
''在调用操作之前添加一点延迟,以允许双击
和按钮已被释放,以发生“”
self.double\u单击\u标志,self.button\u释放\u标志=假,假
自我保护后(300,自我保护,事件)
def双击(自身、事件):
''双击时设置标志''
self.double\u单击\u标志=真
def按钮_已释放(自身、事件):
''释放按钮时设置标志''
self.button\u released\u flag=True
def移动(自身、事件):
''定义鼠标移动时的操作在本例中仅打印
坐标''
打印('鼠标位置在({:03}.{:03}')。
格式(event.x,event.y),end='\r')
def操作(自身、事件):
''单击和双击时定义操作在本例中仅打印
事件“”'
如果self.button_释放了_标志:
如果self.double\u单击\u标志:
打印('鼠标双击事件')
其他:
打印('单次鼠标单击事件')
root=Tk()
窗口=画布(根,宽度=400,高度=400,背景为灰色)
鼠标=鼠标控件(窗口)
窗口位置(x=0,y=0)
window.mainloop()

可能是问这个问题的更好地方;所以主要是针对坏代码。比什么容易?您的解决方案只是几行。你需要多简单?谢谢你的意见。只是我花了一段时间才得到这个解决方案,特别是有点延迟的技巧,所以我想一定有更直接的方法。