Zig*_*gnd 2 python button pygobject python-3.x gtk3
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()
Run Code Online (Sandbox Code Playgroud)
改编自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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1113 次 |
| 最近记录: |