3

我正在尝试使用 GtkClutter 编写一个应用程序,但我无法让演员发出信号。这是一个示例

from gi.repository import GtkClutter, Clutter, Gtk, Gdk

GtkClutter.init([])

class MyCanvas(GtkClutter.Embed):
    __gtype_name__ = 'MyCanvas'

    def __init__(self):
        super(MyCanvas, self).__init__()

        self.stage = self.get_stage()

        self.rect = MyRect()
        self.stage.connect('key-press-event', self.key_press) # This Works!
        self.stage.add_actor(self.rect)
        self.rect.set_position(10, 10)

        self.show_all()

    def key_press(self, widget, event):
        print widget, event


class MyRect(Clutter.Rectangle):
    __gtype_name__ = 'MyRect'

    def __init__(self):
        super(MyRect, self).__init__()
        self.set_color(Clutter.Color.new(255, 255, 255, 0))
        self.set_border_color(Clutter.Color.new(255, 255, 255, 255))
        self.set_border_width(1)
        self.set_size(200, 200)

        self.set_reactive(True)

    # None of the following callbacks work
    # Not even if I explicitly connect signals like 
    # self.connect('button-press-event', self.on_button_press)

    def do_button_press_event(self, event):
        print event

    def do_motion_event(self, event):
        print event


class MyWindow(Gtk.Window):
    __gtype_name__ = 'MyWindow'

    def __init__(self):
        super(MyWindow, self).__init__()
        self.canvas = AnnotateCanvas()
        self.set_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON1_MOTION_MASK)
        self.set_size_request(500, 500)

        vbox_main = Gtk.VBox()
        scrolledwin = Gtk.ScrolledWindow()
        scrolledwin.add_with_viewport(self.canvas)
        vbox_main.pack_end(scrolledwin, True, True, 0)
        self.add(vbox_main)

    def run(self):
        self.show_all()
        self.loop = GObject.MainLoop()
        self.loop.run()


app = MyWindow()
app.run()

所有信号都工作正常,但像“按钮按下”和“运动”这样的鼠标信号没有在杂乱演员 (MyRect/MyCanvas.stage) 对象上触发。即使我放弃子分类并使用股票类做所有事情也不会。

我想 GtkWindow 或 GtkEmbed 不允许鼠标事件传播到 ClutterStage

简化版

from gi.repository import GObject, GtkClutter, Clutter, Gtk

GtkClutter.init([])

def pressed(widget, event):
    print widget, event

w = Gtk.Window()
e = GtkClutter.Embed.new()
e.set_size_request(500, 500)
w.add(e)

s = e.get_stage()
s.set_color(Clutter.Color.new(0,0,0,0))
s.set_size(500, 500)

r = Clutter.Rectangle()
r.set_color(Clutter.Color.new(255,255,255,255))
r.set_size(200,200)
r.set_position(0,0)
r.set_reactive(True)
s.add_actor(r)

s.connect('button-press-event', pressed)
# r.connect('button-press-event', pressed)
w.show_all()
GObject.MainLoop().run()

仅使用 Clutter 时,舞台和演员会发出鼠标信号。使用 GtkClutter.Embed 将舞台嵌入 GtkWindow 时,它只是不起作用

更新

当我尝试从C 中的http://www.openismus.com/documents/clutter_tutorial/0.9/docs/tutorial/html/sec-stage-widget.html运行示例时,它运行良好但将其转换为python ( https://gist.github.com/1759658 ) 根本不起作用。

通过不工作,我的意思是舞台不会触发“按钮按下事件”

4

1 回答 1

6

我认为这与GtkClutter.init()必须在Gtk.init()and之前调用的事实有关Clutter.init(),但后两者是在导入 Gtk 和 Clutter 包时调用的。

像这样导入包似乎使您的示例工作:

from gi.repository import GtkClutter
GtkClutter.init([])
from gi.repository import Clutter, GObject, Gtk
于 2012-02-07T20:35:30.907 回答