绑定到double_click事件时,tkinter的行为异常-什么是双击? [英] tkinter behaves unexpectedly when binding to double_click event - what is a double click?

查看:103
本文介绍了绑定到double_click事件时,tkinter的行为异常-什么是双击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将tk 8.6与python 3.5配合使用.

I'm using tk 8.6 with python 3.5.

我正在编写一个需要同时单击和双击的应用程序,有时双击可能会非常靠近地发生.

I'm writing an application where I need to act on both single and double clicks, and sometimes double clicks can occur very close together.

tk文档指出,当您同时绑定到单击事件和双击事件时,第一次单击将生成一个Button-1事件,而第二次单击将生成Double-Button-1事件.这是我双击一次时看到的.

The tk documentation states that when you bind to both single and double click events, the first click generates a Button-1 event, and the second a Double-Button-1 event. And this is what I observe when I double click once.

当我重复双击时,我希望看到以下事件-单,双,单,双.这是我在两次双击之间留出明显的停顿时所看到的.当我一个接一个地重复时,我看到一个,一个,一个,一个,另一个.当我放慢操作速度时,看到一个或另一个的阈值大约是区分两次单击与两次单击的单击间延迟.

When I repeat the double click, I would expect to see the following events - single, double, single, double. This is what I see when I leave a significant pause between the two double clicks. When I repeat one quickly after the other, I see single, double, double, double. As I slow the operation down, the threshold for seeing one or the other is roughly the inter-click delay for discriminating a double click from two singles.

看来,tkinter会将快速跟随另一个点击的任何点击视为双击.我希望它可以将第一次点击与第二次点击是否合格相匹配,如果是,则将第三次"点击视为一次点击.这种行为意味着,两次双击产生的事件顺序会根据两次间隔间的延迟而变化.

It appears that tkinter treats any click following quickly on from another as a double click. I would have expected it to qualify the first click with whether it was the second click of a double, and if it was, to treat the 'third' click as a single click. This behaviour means that the sequence of events generated by a pair of double clicks varies depending on the inter-double delay.

要获得一致的行为,我的解决方法是停止绑定到double,为单打计时,并为双击做自己的延迟阈值.我认为,这比尝试解释双击是否是真正的"双击更为干净.

My workaround to get consistent behaviour has been to stop binding to double, time the singles, and do my own delay thresholding for double clicks. This I figured was cleaner than trying to interpret whether doubles were 'really' double clicks.

此行为是tkinter中的错误吗?

Is this behaviour a bug in tkinter?

在这种情况下其他GUI管理器会做什么?

What do other GUI managers do under these circumstances?

在这种情况下应该怎么办?

Is there a definition of what should happen under these circumstances?

/edit/我刚刚引起了我的注意,tk也支持三重和四重单击.但是,我仍然认为tk的行为出乎意料.如果仅绑定一次单击,则我的两次双击"(基本上是四次单击)会生成四个单事件.因此,我希望仅绑定到单击和双击,它应该将单击和双击的四元解释仅解释为单击,双击,单击,双击.我想我最终的程序文档应该提到我不使用三次或三次点击.但是,嘿,我有解决方法.//edit/

/edit/ I've just had it brought to my attention that triple and quadruple clicks are also supported by tk. However, I still think there's something unexpected in tk's behaviour. If I bind to only single click, then my 'two double clicks', basically a quadruple click, generates four single events. Therefore I would expect on binding to single and double only, it should interpret a quadruple in terms of single and double clicks only as single, double, single, double. I guess my eventual program documentation should mention that I don't use triple or quad clicks. But hey, I've got my workaround. //edit/

/edit2/我已经绑定了三次和四次点击,以查看会发生什么,并且它会出现更多的错误.我从来没有四次点击四次事件.我确实收到了一系列的单,双,三,双事件.我也有一个,一个,一个,三个,三个三倍,而AFAICS的输入时间相同.我会坚持只绑定到单身人士,并按自己的时间安排!//edit2/

/edit2/ I've bound to triple and quadruple clicks to see what happens, and it gets more buggy. I never get a quad event with a quad click. I do get a sequence of single, double, triple, double events delivered. I've also had a single, double, triple, triple, with AFAICS the same input timing. I'll stick to binding to singles only, and doing my own timing! //edit2/

推荐答案

出于文档:

"Double","Triple"和"Quadruple"修饰符可方便地指定双击鼠标和其他重复事件.它们会导致特定的事件模式重复2、3或4次,并对序列施加时间和空间要求:对于与Double,Triple或Quadruple pattern 模式匹配的事件序列,所有事件必须发生在时间上靠得很近并且之间没有实质的鼠标移动.例如,在需要额外的时间和空间的情况下,< Double-Button-1> 等效于< Button-1>< Button-1> .

The Double, Triple and Quadruple modifiers are a convenience for specifying double mouse clicks and other repeated events. They cause a particular event pattern to be repeated 2, 3 or 4 times, and also place a time and space requirement on the sequence: for a sequence of events to match a Double, Triple or Quadruple pattern, all of the events must occur close together in time and without substantial mouse motion in between. For example, <Double-Button-1> is equivalent to <Button-1><Button-1> with the extra time and space requirement.

-> 多次匹配

如果一个特定的事件与多个绑定匹配,并且它们具有相同的标签,则选择最具体的绑定并评估其脚本.为了确定几个匹配序列中的哪一个更具体,应用了以下测试:

If more than one binding matches a particular event and they have the same tag, then the most specific binding is chosen and its script is evaluated. The following tests are applied, in order, to determine which of several matching sequences is more specific: 

(c)如果在一个模式中指定的修饰符是另一模式中修饰符的子集,则具有更多修饰符的模式更为具体.

(c) if the modifiers specified in one pattern are a subset of the modifiers in another pattern, then the pattern with more modifiers is more specific. 

所以这应该起作用:

def func2(event):
    print('double')
    widget.unbind('<Double-1>')

def func1(event):
    print('single')
    widget.bind('<Double-1>', func2)

widget.bind('<Button-1>', func1)

修改

它有效,请尝试:

import tkinter as tk

root = tk.Tk()

up = tk.Frame(root)
up.grid(column=0, row=0,columnspan=3,sticky='n')
s1 = tk.Label(up, text='spacer')
s1.pack(fill='both',expand=1)

def func4(event):
    print('Quadruple')
    s1.unbind('<Quadruple-1>')

def func3(event):
    print('triple')
    s1.unbind('<Triple-1>')
    s1.bind('<Quadruple-1>', func4)

def func2(event):
    print('double')
    s1.unbind('<Double-1>')
    s1.bind('<Triple-1>', func3)
    

def func1(event):
    print('single')
    s1.bind('<Double-1>', func2)

s1.bind('<Button-1>', func1)

root.mainloop()

这篇关于绑定到double_click事件时,tkinter的行为异常-什么是双击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆