我下面就使用林間空地和VALA的教程,可以發現heregdk_device_get_source!= GDK_SOURCE_KEYBOARD
我已經按照說明操作,並確認我的代碼是一樣的例子。
從瓦拉的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,我得到的窗口(根據例如):
所以這一切都很好。但是,當我嘗試點擊文本字段,我在控制檯得到一個GTK的嚴重錯誤消息:
(例如:16109):GDK-CRITICAL **:gdk_device_get_n_axes:斷言「gdk_device_get_source(設備)! = 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)
謝謝你的工作。花了我一些時間來弄清楚如何設置屬性,所以我編輯了你的答案並添加了可用的代碼。 – Metalskin