【问题标题】:Error while trying to access city name using GWeather and gtk尝试使用 GWeather 和 gtk 访问城市名称时出错
【发布时间】:2021-04-03 08:02:00
【问题描述】:

我想访问城市名称并将其添加到 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.

【问题讨论】:

    标签: location gtk vala


    【解决方案1】:

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

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

    【讨论】:

    • 我在 meson.build 文件中做了这个。 ``` c_args = [ '-DGWEATHER_I_KNOW_THIS_IS_UNSTABLE', ] ``` 但它仍然显示相同的消息。
    • 您是否将它们添加到您的executable() 中,例如第 81 行? gitlab.gnome.org/GNOME/gnome-clocks/-/blob/master/src/…
    • 现在可以使用了,非常感谢您分享 GNOME 时钟项目。它和我正在尝试构建的非常相似,我会复制很多代码,呵呵。我非常感谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多