如何将两个不同的按钮单击事件绑定到同一标签,即“< Button-1>"按钮.和“< Double-Button1>"到同一标签? [英] How would one bind two different button click events to the same label, namely "<Button-1>" and "<Double-Button1>" to same label?

查看:106
本文介绍了如何将两个不同的按钮单击事件绑定到同一标签,即“< Button-1>"按钮.和“< Double-Button1>"到同一标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道必须有一种方法可以做到这一点,即我整天都在努力寻找解决方案,而这是我所得到的最接近的解决方案.该代码有效,但缺点是它永远不会到达else if语句,因为在该语句中首先单击的任何按钮都将始终为True,因此它将永远不会到达else if.他们有某种方法可以将我的代码的前两个语句组合成一个,因为我相信这样可以解决?这是使用tkinter GUI.

So, i know there has got to be a way to do this iv'e literally been trying all day to figure a solution and this is the closest i've come to getting it. The code works but the flaw is that it never reaches the else if statement because whatever button click is first in the statement will always be True hence it will never reach else if. Is their some way to combine the first two statements of my code into one because i believe that would solve it? This is using the tkinter GUI.

    self.label1.bind("<Double-Button-1>",self._two_click)
    self.label1.bind("<Button-1>", self._two_click)

def  _two_click(self,event):
    if self.label1.bind("<Button-1>"):
        self.label1.configure(self.my_timeBB())
    elif self.label1.bind("<Double-Button-1>"):
        self.label1.configure(text="Blue")

推荐答案

我在函数中使用了额外的参数来识别点击.

I use extra argument in function to recognize click.

BTW:您始终可以将< Button-1> < Double-Button1> 绑定到一个小部件,但功能不同吗?

BTW: you can always bind <Button-1> and <Double-Button1> to one widget but with different functions ?

import Tkinter as tk

def test(event, extra=None):
    print extra

master = tk.Tk()

b1 = tk.Button(master, text="QUIT", command=master.destroy, width=20, heigh=5)
b1.pack()

b2 = tk.Label(master, text="OK", width=20, heigh=5)
b2.pack()

b2.bind('<Double-Button-1>', lambda event:test(event,101))

b2.bind('<Button-1>', lambda event:test(event,1))
b2.bind('<Button-2>', lambda event:test(event,2))
b2.bind('<Button-3>', lambda event:test(event,3))

master.mainloop()

但是我看到一个(大)问题-当我尝试用鼠标双击时,我总是得到两个文本-第一个是单击,第二个是双击.

But I see one (big) problem - when I try to make double-click with my mouse I always get two text - first for single-click and second for double-click.

唯一的解决方案是测量单击之间的时间,并决定选择单击还是双击.但是可能也需要使用 after().

The only solution can be measure time between click and decide whether choose single or double click. But probably it will need to use after() too.

仅运行单次或两次点击

import Tkinter as tk

#----------------------------------------------------------------------

single = False

def test(event, extra=None):
    global single

    #print 'event-num:', event.num
    #print 'extra:', extra

    if extra == 1:
        single = True
        master.after(200, single_click)
    elif extra == 101:
        single = False
        click('double click')

def single_click():
    global single

    if single:
        single = False
        click('single click')

def click(msg):
    print 'It was', msg

#----------------------------------------------------------------------

master = tk.Tk()

b1 = tk.Button(master, text="QUIT", command=master.destroy, width=20, heigh=5)
b1.pack()

b2 = tk.Label(master, text="OK", width=20, heigh=5)
b2.pack()

b2.bind('<Double-Button-1>', lambda event:test(event,101))
b2.bind('<Button-1>', lambda event:test(event,1))

master.mainloop()

这篇关于如何将两个不同的按钮单击事件绑定到同一标签,即“&lt; Button-1&gt;"按钮.和“&lt; Double-Button1&gt;"到同一标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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