处理pygtk中的双击和单击事件

时间:2014-04-05 13:33:32

标签: python gtk pygtk glib double-click

我正在尝试使用glib.timeout_add()glib.source_remove()将单击事件与双击事件分开。我是这样做的:

class Exchange:

'''

some code

'''

    def __init__(self):

        self.timeoutID_1 = 0
        self.timeoutID_2 = 0

        self.startTime1 = 0.0
        self.stopTime1 = 0.0
        self.startTime2 = 0.0
        self.stopTime2 = 0.0

        '''

        some code

        '''

        ## THE DOUBLE CLICK SIGNAL - item-activated
        iconView1.connect("item-activated", self.on_item_activated_1,upButton1, store1)
        sw1.add(iconView1)

        iconView2.connect("item-activated", self.on_item_activated_2,upButton2, store2)
        sw2.add(iconView2)

        ## THE SINGLE CLICK SIGNAL - selection-changed

        iconView1.connect("selection-changed", self.on_selection_changed_1, copyButton1, cutButton1, pasteButton1, deleteButton1)
       iconView2.connect("selection-changed", self.on_selection_changed_2, copyButton2, cutButton2, pasteButton2, deleteButton2)

    def on_selection_changed_1(self, iconView1, copyButton1, cutButton1, pasteButton1, deleteButton1) :


        self.startTime1 = time.time()

        self.timeoutID_1 = glib.timeout_add(2000, self.selectIcon_1, iconView1, copyButton1, cutButton1, pasteButton1, deleteButton1)


    def on_selection_changed_2(self, iconView2, copyButton2, cutButton2, pasteButton2, deleteButton2) :

        self.startTime2 = time.time()

        self.timeoutID_2 = glib.timeout_add(2000, self.selectIcon_2, iconView2, copyButton2, cutButton2, pasteButton2, deleteButton2)


    def selectIcon_1(self, iconView1, copyButton1, cutButton1, pasteButton1, deleteButton1) :

        copyButton1.set_sensitive(True)
        cutButton1.set_sensitive(True)
        pasteButton1.set_sensitive(True)
        deleteButton1.set_sensitive(True)


    def selectIcon_2(self, iconView2, copyButton2, cutButton2, pasteButton2, deleteButton2) :


        copyButton2.set_sensitive(True)
        cutButton2.set_sensitive(True)
        pasteButton2.set_sensitive(True)
        deleteButton2.set_sensitive(True)

    def on_item_activated_1(self, iconView1, item, upButton1, store1) :

        self.stopTime1 = time.time()

        if self.stopTime1 - self.startTime1 < 1.50 :
            glib.source_remove(self.timeoutID_1)

        '''

        some code

        '''

    def on_item_activated_2(self, iconView2, item, upButton2, store2) :

        self.stopTime2 = time.time()

        if self.stopTime2 - self.startTime2 < 1.50 :
            glib.source_remove(self.timeoutID_2)

        '''

        some code

        '''

尽管self.stopTime - self.startTime <1.50True(表示有效的双击),但每次单击都会执行单击事件,但仅在2秒后执行一次。如何完全取消有效双击的selectIcon方法的执行?

更新

根据mtwebster的回答,我尝试使用button_press_event。可悲的是,虽然我回到原点。

def on_button_press_event(self, widget, event) :
    if event.button == 1 :
        data = widget.get_path_at_pos(int(event.x), int(event.y))
        if data :
            if event.type == gtk.gdk._2BUTTON_PRESS :
                print " double click "

            elif event.type == gtk.gdk.BUTTON_PRESS :
                print " single click "

OUTPUT ::

vineet@vineet:~/Documents/Project$ python draft6.py
 single click 
 single click 
 double click 

添加到我的困境中正在执行三次点击事件,双击,两次单击和一次双击!有没有其他的时机机制我可以使用而不是glib.timeout_add()我不必处理重复调用废话的不确定性?

3 个答案:

答案 0 :(得分:0)

我认为这不是一个真正有效的答案......但你是否考虑过连接到IconView的'on-button-press'信号?

在事件回调中,您可以让Gdk通过检查event-&gt;类型来判断它是双击事件还是单击(请参阅https://developer.gnome.org/gdk3/stable/gdk3-Event-Structures.html#GdkEventButton)。

答案 1 :(得分:0)

我不认为删除处理程序是一个很好的策略。

我曾经做过一次这样的黑客攻击 - 将单个点击(即点击选择)的不同逻辑附加到一起,然后双击(即点击执行)到TreeView中的一行。此外,您可能希望支持键盘导航和图像。

我设法让它在我当时工作的pygtk版本中运行。然而,稍后发布了一些小的gtk,我的代码破了。事实证明,不可能支持具有相同代码的多个版本的gtk。

最实用的方法是跟踪2项:

  • 是点击已经选中的对象(在我的情况下是行)
  • 选择此行的方式和时间
      默认情况下选择了
    • 行(显示/切换视图时的第一行) - 忽略
    • 键盘选择
    • 行 - 忽略
    • 很久以前就选择了
    • 行(比如300ms) - 忽略
    • else(通过点击选择,小于300毫秒) - 执行

答案 2 :(得分:0)

您可以安排单击事件在双击持续时间之后发生,然后如果发生双击,则在运行处理程序之前删除计划的单击事件。例如:

import pygtk
pygtk.require('2.0')
import gtk
import glib
class Test:
    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Test")

        # It's a good idea to do this for all windows.
        self.window.connect("destroy", lambda wid: gtk.main_quit())
        self.window.connect("delete_event", lambda a1,a2:gtk.main_quit())

        # Sets the border width of the window.
        self.window.set_border_width(20)

        # Create a new button
        button = gtk.Button("Click")
        self.click_events = []

        # Connect the "clicked" signal of the button to our callback
        button.connect("button-press-event", self.on_button_press_event)
        button.show()

        self.window.add(button)
        self.window.show()

    def on_button_press_event(self, widget, event):
        if event.button == 1:
            if event.type == gtk.gdk._2BUTTON_PRESS:
                # Remove all single click events
                while self.click_events:
                    glib.source_remove(self.click_events.pop()) 
                glib.idle_add(self.handle_double_click,widget,event)
            elif event.type == gtk.gdk.BUTTON_PRESS:
                # Schedule after double click can occur
                self.click_events.append(glib.timeout_add(300,self.handle_single_click,widget,event))


    def handle_single_click(self,widget,event):
        # TODO: Handle single click
        print "TODO: Handle single click"


    def handle_double_click(self,widget,event):
        # TODO: Handle double click
        print "TODO: Handle double click"

def main():
    Test()
    gtk.main()
    return 0     

if __name__ == "__main__":
    main()