【问题标题】:Update Gtk.Label dynamicall when time changes时间变化时更新 Gtk.Label dynamicall
【发布时间】:2021-09-26 06:58:37
【问题描述】:

我正在使用此代码将当前时间写入应用中的 Gtk.Label。

public bool update_time () {
        var now = new GLib.DateTime.now_local ();
        var settings = new GLib.Settings ("org.gnome.desktop.interface");
        var time_format = Granite.DateTime.get_default_time_format (settings.get_enum ("clock-format") == 1, false);

        time1_label = new Gtk.Label (now.format (time_format)) {
            halign = Gtk.Align.CENTER,
            valign = Gtk.Align.CENTER,
            margin_top = 5
        };
        time1_label.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
        time1_label.tooltip_text = time_format;
        time1_label.xalign = 0;

        return true;
    }
    

时间显示正确,但我希望标签在更改时更新为最新时间。我该怎么做?

也许以某种方式使用Timeout,但我不知道如何。

【问题讨论】:

    标签: gtk glib vala


    【解决方案1】:

    您必须创建一个超时,并在其回调中更改time1_label 的文本。 (使用Gdk.threads_add_timeout() 添加超时回调,以防任何依赖项可能使用已弃用的 GTK 锁定机制。)

    【讨论】:

      【解决方案2】:

      您可以像本例中那样使用超时:

      public class RefreshLabel : Gtk.Application {
              
          private uint[] timeout_id;
          
          private Gtk.Label label;
          
          public RefreshLabel () {
              Object (
                  application_id: "refreh.my.label",
                  flags: ApplicationFlags.FLAGS_NONE
              );
          }
      
          protected override void activate () {
              Gtk.ApplicationWindow window = new Gtk.ApplicationWindow (this);
              window.set_default_size (100, 50);
              window.window_position = Gtk.WindowPosition.CENTER;
              window.set_border_width(10);        
              
              // create the timeout with your callback that update the label every 1 second
              timeout_id += Timeout.add_seconds_full (GLib.Priority.DEFAULT, 1, update_time);
              
              label = new Gtk.Label ("");
              var now = new GLib.DateTime.now_local ();
              label.set_markup ("<big>" + now.format ("%x %X") + "</big>");
              
              Gtk.Box vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
              vbox.pack_start (label, false, false, 0);
      
              window.add (vbox);
              window.show_all ();
          }
      
          protected override void shutdown () {
              // On close all instance of the timeout must be closed
              foreach (var id in timeout_id)
                  GLib.Source.remove (id);
              base.shutdown ();
          }
          
          public bool update_time () {
              var now = new GLib.DateTime.now_local ();
              label.set_markup ("<big>" + now.format ("%x %X") + "</big>");
              return true;
          }
      
          public static int main (string[] args) {
              RefreshLabel app = new RefreshLabel ();
              return app.run (args);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-30
        • 2019-03-31
        • 1970-01-01
        • 1970-01-01
        • 2019-03-23
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        相关资源
        最近更新 更多