我试图抓住点击的协调。我创建了Gtk.Window,EventBox并将Image放入,将一些EventButton连接到button_press_event并将一些方法连接到此信号。此方法尝试从EventButton对象获取x和y,但它们始终为0.有源代码:
using Gtk;
public class ComixTranslator : Window
{
private Gtk.Image CurPage;
private Gdk.Pixbuf Canvas;
private Gtk.EventBox eventbox1;
private Gdk.EventButton ebutton1;
public bool FillDot() {
GLib.message("pressed in %g,%g",ebutton1.x,ebutton1.y);
return true;
}
public ComixTranslator () {
this.title = "Image Click Sample";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect(Gtk.main_quit);
this.set_default_size(800,600);
this.CurPage = new Gtk.Image();
this.CurPage.set_from_file("test.jpg");
this.Canvas = CurPage.pixbuf;
this.eventbox1 = new Gtk.EventBox();
this.eventbox1.button_press_event(ebutton1);
this.eventbox1.button_press_event.connect(FillDot);
this.eventbox1.add(CurPage);
this.add(eventbox1);
}
public static int main(string[] args) {
Gtk.init(ref args);
ComixTranslator MainWindow = new ComixTranslator();
MainWindow.show_all();
Gtk.main();
return 0;
}
}
答案 0 :(得分:3)
您似乎对信号如何工作感到困惑 - 您可能想要考虑阅读that part of the Vala Tutorial。这是一个更正版本:
using Gtk;
public class ComixTranslator : Window
{
private Gtk.Image CurPage;
private Gdk.Pixbuf Canvas;
private Gtk.EventBox eventbox1;
public bool FillDot(Gtk.Widget sender, Gdk.EventButton evt) {
GLib.message("pressed in %g,%g",evt.x,evt.y);
return true;
}
public ComixTranslator () {
this.title = "Image Click Sample";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect(Gtk.main_quit);
this.set_default_size(800,600);
this.CurPage = new Gtk.Image();
this.CurPage.set_from_file("test.jpg");
this.Canvas = CurPage.pixbuf;
this.eventbox1 = new Gtk.EventBox();
this.eventbox1.button_press_event.connect(FillDot);
this.eventbox1.add(CurPage);
this.add(eventbox1);
}
public static int main(string[] args) {
Gtk.init(ref args);
ComixTranslator MainWindow = new ComixTranslator();
MainWindow.show_all();
Gtk.main();
return 0;
}
}
请注意,回调接受Gdk.EventButton作为参数。在您的代码中,您有this.eventbox1.button_press_event(ebutton1);
,它将发出 button_press_event信号,并将来自ebutton1的数据作为其参数。