gdk_device_get_source!= GDK_SOURCE_KEYBOARD

时间:2014-02-16 02:03:26

标签: gtk glade vala

我正在学习使用glade和vala的教程,可以找到here

我按照说明操作并确认我的代码与示例相同。

Vala的xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <signal name="destroy" handler="on_window1_destroy" swapped="no"/>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkEntry" id="entry1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="invisible_char">•</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_button1_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface> 

我写的代码如下:

using Gtk;

/* When button click signal received */
public void on_button1_clicked (Button source) {
    /* change button label to clicked! */
    source.label = "Clicked!";
    stderr.printf ("Clicked! --> ");
}

/* when window close signal received */
public void on_window1_destroy (Window source) {
    Gtk.main_quit();
}

int main (string[] args) {
    Gtk.init (ref args);

    var builder = new Builder ();

    /* getting the glade file */
    builder.add_from_file ("sample.ui");
    builder.connect_signals (null);

    var window = builder.get_object ("window1") as Window;
    var entry = builder.get_object ("entry1") as Entry;
    var button = builder.get_object ("button1") as Button;

    /* that's another way to do something when signal received */
    button.clicked.connect (() => {
        stderr.printf ("%s\n", entry.get_text ());
    });
    window.show_all ();
    Gtk.main ();

    return 0;
}

当我运行已编译的vala时,我得到了窗口(根据示例):

enter image description here

所以这一切都很好。但是,当我尝试单击文本字段时,我在控制台中收到Gtk-CRITICAL错误消息:

  

(例如:16109):Gdk-CRITICAL **:gdk_device_get_n_axes:断言'gdk_device_get_source(device)!= GDK_SOURCE_KEYBOARD'失败

我没有把注意力集中在这个领域,因此我不能在场上输入任何东西。我不确定为什么会这样。我已经回顾了我所做的事情,我无法看到我出错的地方。

顺便说一句,当我编译时,我注意到以下警告:

$ valac --pkg gtk+-3.0 --pkg gmodule-2.0 example.vala
example.vala:22.5-22.39: warning: unhandled error `GLib.Error'
    builder.add_from_file ("sample.ui");
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.vala.c: In function ‘main’:
example.vala.c:141:2: warning: ‘g_type_init’ is deprecated (declared at /usr/include/glib-2.0/gobject/gtype.h:669) [-Wdeprecated-declarations]
  g_type_init ();
  ^
Compilation succeeded - 1 warning(s)

1 个答案:

答案 0 :(得分:3)

您需要将文本条目的can-focus属性设置为True才能对其进行聚焦(尽管我的代码不会出现运行时错误)。

如果您使用的是最新版本的GTK(3.10),您可能需要阅读template/composite widgets,因为这样可以直接节省Gtk.Builder,您只需添加几个注释即可。您。

在GTK3中expand&amp;不推荐使用fill子属性,而是expandhexpandvexpandhalign&amp; valign小部件属性,可以更灵活地分配和使用额外空间。

关于未处理错误的vala警告是因为代码没有捕获错误,例如找不到文件或返回的ui定义中的标记错误builder.add_from_file

不要过于担心第二次警告,vala会为旧版本的glib生成代码而不是已安装的代码。将--target-glib 2.36添加到vala命令行以进行修复。

相关问题