gpt4 book ai didi

click - Vala 图片点击事件。如何获得点击的协调?

转载 作者:行者123 更新时间:2023-12-01 14:59:50 25 4
gpt4 key购买 nike

我尝试捕捉点击的协调。我创建了 Gtk.Window、EventBox 并将图像放入其中,将一些 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;
}
}

最佳答案

您似乎对信号的工作原理感到困惑——您可能需要考虑阅读 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 的数据作为其参数。

关于click - Vala 图片点击事件。如何获得点击的协调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22447698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com