【问题标题】:Vala Image click event. How to get coordination of click?Vala 图片点击事件。如何获得点击的协调?
【发布时间】:2014-03-17 05:44:12
【问题描述】:

我试图捕捉点击的协调。我创建了 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;
}
}

【问题讨论】:

    标签: click gtk coordinates gtk3 vala


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多