2

Gtk.Button 是否有任何事件会在按下按钮时重复执行代码?

假设我有以下用 Python 3 编写并使用 PyGObject 的代码。当用户在按钮上按住鼠标左键(单击并按住)时,我希望在屏幕上重复打印消息“Hi”。

我可以使用任何其他事件来代替单击或任何其他解决方案吗?谢谢。

from gi.repository import Gtk

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.button = Gtk.Button("Hi Printer")
        self.button.connect("clicked", self.on_button_clicked)
        self.add(self.button)
        self.connect("delete-event", Gtk.main_quit)
    def on_button_clicked(self, widget):
        print("Hi")

window = Window()
window.show_all()
Gtk.main()
4

2 回答 2

5

改编自tcaswell的编辑答案,但避免在print_hi()按钮释放后额外调用:

from gi.repository import Gtk, GObject

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.button = Gtk.Button("Hi Printer")
        self.button.connect("pressed", self.on_button_clicked)
        self.button.connect("released", self.on_button_released)
        self.add(self.button)
        self.connect("delete-event", Gtk.main_quit)

    def on_button_clicked(self, widget):
        # kick off time out
        timeout = 50
        self._timeout_id = GObject.timeout_add(timeout, self.print_hi)

    def on_button_released(self, widget):
        # remove timeout
        GObject.source_remove(self._timeout_id)
        self._timeout_id = 0 # better safe than sorry

    def print_hi(self):
        print 'hi'
        # repeat until the source is removed
        return True

window = Window()
window.show_all()
Gtk.main()
于 2013-10-18T04:52:33.240 回答
1
from gi.repository import Gtk, GObject

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.button = Gtk.Button("Hi Printer")
        self.button.connect("pressed", self.on_button_clicked)
        self.button.connect("released", self.on_button_released)
        self.add(self.button)
        self.connect("delete-event", Gtk.main_quit)

    def on_button_clicked(self, widget):
        # arm repeater
        self._repeat = True
        # trigger printing
        self.print_hi()

    def on_button_released(self, widget):
        # disarm repeater
        self._repeat = False

    def print_hi(self):
        print 'hi'
        # tune timeout for repeat rate
        timeout = 50
        # if should still be repeating
        if self._repeat:
            # arm timer, with call back to this function
            GObject.timeout_add(timeout, self.print_hi)

window = Window()
window.show_all()
Gtk.main()

之前的问题是“点击”在按钮上是上下,“按下”是向下,“释放”是向上。

还更新为使用正确的计时器。

事实证明 gtk 中的超时功能可以自动重新设置,因此可以更简单地完成:

from gi.repository import Gtk, GObject


class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.button = Gtk.Button("Hi Printer")
        self.button.connect("pressed", self.on_button_clicked)
        self.button.connect("released", self.on_button_released)
        self.add(self.button)
        self.connect("delete-event", Gtk.main_quit)

    def on_button_clicked(self, widget):
        # arm repeater
        self._repeat = True
        # kick off time out
        timeout = 50
        GObject.timeout_add(timeout, self.print_hi)

    def on_button_released(self, widget):
        # disarm repeater
        self._repeat = False

    def print_hi(self):
        print 'hi'
        # if should still be repeating
        return self._repeat

window = Window()
window.show_all()
Gtk.main()

例子

于 2013-10-17T23:39:05.980 回答