我正在尝试将用python编写的音频播放器迁移到GTK3 +。在GTK2中,我使用了progress_bar.add_event(... pointer_motion_notify | button_press)(下面的完整代码),并为button_press和pointer_motion_notify设置了信号处理程序。在GTK3中,似乎ProgressBar()不会发出这些信号。
我已经使用Overlay()和DrawingArea()实现了一种解决方法,它允许DrawingArea发出信号,但不应该...... 这是一个错误吗?或者我做错了吗?
代码:
import gi
gi.require_version("Gtk","3.0")
from gi.repository import Gtk, Gdk, GObject
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__(title='ProgressBar Event Test')
self.progressbar = Gtk.ProgressBar()
self.add(self.progressbar)
self.progressbar.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
| Gdk.EventMask.POINTER_MOTION_MASK)
self.progressbar.connect("button-press-event", self.on_event, 'button-press')
self.progressbar.connect("motion-notify-event", self.on_event, 'motion-notify')
self.connect("delete-event", Gtk.main_quit)
self.current_progress = 0.0
GObject.timeout_add(200,self.update_progress)
self.show_all()
def on_event(self, widget, event, data=None):
print "on_event called for %s signal"%data
return False
def update_progress(self):
self.progressbar.set_fraction(self.current_progress)
self.current_progress += 0.01
return self.current_progress <= 1 # False cancels timeout
def main():
w = MainWindow()
Gtk.main()
if __name__ == '__main__':
main()
答案 0 :(得分:4)
使用事件框可能更好 - 您将窗口小部件添加到事件框并自行连接到事件框信号。
因此:
import gi
gi.require_version("Gtk","3.0")
from gi.repository import Gtk, Gdk, GObject
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__(title='ProgressBar Event Test')
eventbox = Gtk.EventBox()
self.progressbar = Gtk.ProgressBar()
eventbox.add(self.progressbar)
self.add(eventbox)
eventbox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
| Gdk.EventMask.POINTER_MOTION_MASK)
eventbox.connect("button-press-event", self.on_event, 'button-press')
eventbox.connect("motion-notify-event", self.on_event, 'motion-notify')
self.connect("delete-event", Gtk.main_quit)
self.current_progress = 0.0
GObject.timeout_add(200,self.update_progress)
self.show_all()
def on_event(self, widget, event, data=None):
print "on_event called for %s signal"%data
return False
def update_progress(self):
self.progressbar.set_fraction(self.current_progress)
self.current_progress += 0.01
return self.current_progress <= 1 # False cancels timeout
def main():
w = MainWindow()
Gtk.main()
if __name__ == '__main__':
main()
有关Gtk.Eventbox的更多信息,请访问here。