尝试使用 GWeather 和 gtk 访问城市名称时出错

问题描述

我想访问城市名称并将其添加到 Gtk.Label。这就是我尝试这样做的方式。

谁能告诉我我的代码有什么问题?或者建议一些其他方式来获取城市名称

public class Epoch.LabelsGrid : Gtk.Grid {
    public Gtk.Label face1_label;
    public Gtk.Label face2_label;
    public Gtk.Label face3_label;
    public Gtk.Label face4_label;
    
    private GWeather.Location location;
    
    construct {
        get_location.begin ();
        
        face1_label = new Gtk.Label ("");
        face1_label.label = location.get_city_name ();
        face1_label.halign = Gtk.Align.CENTER;
        face1_label.hexpand = true;
        face1_label.margin_top = 6;
        face1_label.set_ellipsize (END);
        face1_label.set_max_width_chars (12);
    }
    
    public async void get_location () {
        try {
            var simple = yield new GClue.Simple ("com.github.Suzie97.epoch",GClue.AccuracyLevel.CITY,null);

            simple.notify["location"].connect (() => {
                on_location_updated (simple.location.latitude,simple.location.longitude);
            });

            on_location_updated (simple.location.latitude,simple.location.longitude);
        } catch (Error e) {
            warning ("Failed to connect to GeoClue2 service: %s",e.message);
            return;
        }
    }
    
    public void on_location_updated (double latitude,double longitude) {
        location = GWeather.Location.get_world ();
        location = location.find_nearest_city (latitude,longitude);
    }
}

这是编译时显示错误

[1/2] Compiling C object 'com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o'.
Failed: com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o 
cc -Icom.github.Suzie97.epoch@exe -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/libgeoclue-2.0 -I/usr/include/libgweather-3.0 -I/usr/include/geocode-glib-1.0 -I/usr/include/libsoup-2.4 -I/usr/include/libxml2 -I/usr/include/granite -I/usr/include/gee-0.8 -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -w -g '-DGETTEXT_PACKAGE="com.github.Suzie97.epoch"' -pthread  -MD -MQ 'com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o' -MF 'com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o.d' -o 'com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o' -c 'com.github.Suzie97.epoch@exe/src/Widgets/Labels.c'
In file included from com.github.Suzie97.epoch@exe/src/Widgets/Labels.c:28:0:
/usr/include/libgweather-3.0/libgweather/gweather.h:25:2: error: #error "libgweather should only be used if you understand that it's subject to change,and is not supported as a fixed API/ABI or as part of the platform"
 #error "libgweather should only be used if you understand that it's subject to change,and is not supported as a fixed API/ABI or as part of the platform"
  ^~~~~
ninja: build stopped: subcommand Failed.

解决方法

您需要将 -DGWEATHER_I_KNOW_THIS_IS_UNSTABLE 添加到 C 编译参数中。如果您使用的是介子,GNOME Clocks has a good example

请确保您了解库作者让您这样做的原因。库 API/ABI 将来可能仍会发生变化,如果发生这种情况,您可能需要相应地更新代码。 (例如,有一个删除所有 GTK 小部件的开放合并请求,以便库的其余部分可以与 GTK4 应用程序链接)。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...