检测GTK窗口何时完成用户移动/调整大小

时间:2013-09-27 19:12:58

标签: python c gtk pygobject gdk

我想检测用户何时完成重新调整大小或移动GTK窗口的时间。基本上相当于Windows中的WM_EXITSIZEMOVE

我查看了GTK detecting window resize from the user,并且能够使用configure-event检测大小/位置更改;但是由于我的其他代码是架构师,我想知道何时完成调整大小。几乎像ValueChanged而不是ValueChanging事件。

我在想是否能找到鼠标按钮是否被释放然后尝试检测它是否是我收到的最后一个事件。但无法为窗口对象找到一种方法。

2 个答案:

答案 0 :(得分:2)

您可以使用在调整大小完成后调用的超时函数。超时以毫秒为单位,您可能希望使用该值来在调用resize_done的延迟和在完成调整大小之前触发它之间取得平衡。

#define TIMEOUT 250

gboolean resize_done (gpointer data)
{
  guint *id = data;
  *id = 0;
  /* call your existing code here */
  return FALSE;
}

gboolean on_configure_event (GtkWidget *window, GdkEvent *event, gpointer data)
{
  static guint id = 0;
  if (id)
    g_source_remove (id);
  id = g_timeout_add (TIMEOUT, resize_done, &id);
  return FALSE;
}

答案 1 :(得分:0)

我使用PyGObject和Python 3实现了一个解决方案(有人称其为变通方法)。 正如本问题中的Phillip Wood和另一个问题中的ratchet freak一样,我还使用了基于 timer 的解决方案。

在示例中,size-allocate事件用于检测 窗口大小调整的开始。我假设用户拖动 鼠标在这里与窗口边框。还有其他检测窗口大小调整事件的方法:see discussion here

接下来,事件将断开连接,并作为计时器事件的替代(GLib.timeout_add(),持续500毫秒) 创建用于处理下一个内容。

这是示例代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib


class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        # close event
        self.connect('delete-event', self._on_delete_event)

        # "resize" event
        self._connect_resize_event()

    def _connect_resize_event(self):
        self._timer_id = None
        eid = self.connect('size-allocate', self._on_size_allocated)
        self._event_id_size_allocate = eid

    def _on_delete_event(self, a, b):
        Gtk.main_quit()

    def _on_size_allocated(self, widget, alloc):
        print('EVENT: size allocated')

        # don't install a second timer
        if self._timer_id:
            return

        # remember new size
        self._remembered_size = alloc

        # disconnect the 'size-allocate' event
        self.disconnect(self._event_id_size_allocate)

        # create a 500ms timer
        tid = GLib.timeout_add(interval=500, function=self._on_size_timer)
        # ...and remember its id
        self._timer_id = tid

    def _on_size_timer(self):
        # current window size
        curr = self.get_allocated_size().allocation

        # was the size changed in the last 500ms?
        # NO changes anymore
        if self._remembered_size.equal(curr):  # == doesn't work here
            print('RESIZING FINISHED')
            # reconnect the 'size-allocate' event
            self._connect_resize_event()
            # stop the timer
            self._timer_id = None
            return False

        # YES size was changed
        # remember the new size for the next check after 500ms
        self._remembered_size = curr
        # repeat timer
        return True


if __name__ == '__main__':
    window = MyWindow()
    window.show_all()
    Gtk.main()