C GTK+启动时的事件信号

发布于 2024-10-31 16:20:36 字数 1848 浏览 0 评论 0原文

我正在写一个倒计时器。它由一个带有标签的窗口组成,其中显示时间。 “timer()”(在下面的代码中)作为线程生成。这在linux环境下工作得很好,但据我所知GTK不支持windows下的多线程(如果我错了请纠正我)。是否可以在不使用线程的情况下生成“timer()”,例如通过在 main_window 出现时调用信号?

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <gtk/gtk.h>

GtkWidget *window;
GtkWidget *label;
pthread_t timer_thread;

void* timer()
{
    while(1)
    {
        // some code here to calculate time
        gtk_label_set_markup ((GtkLabel*)label, time_c);
        sleep(1);
    }
}

static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data )
{
    return FALSE;
}

static void destroy( GtkWidget *widget, gpointer data )
{
    gtk_main_quit ();
}

int main(int argc, char **argv)
{
    g_thread_init(NULL);
    gdk_threads_init();  
    gdk_threads_enter();
    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    g_signal_connect (window, "delete-event", G_CALLBACK (delete_event), NULL);
    g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);

    label = gtk_label_new("");

    gtk_container_add (GTK_CONTAINER (window), label);
    gtk_widget_show (label);
    gtk_widget_show (window);

    pthread_create(&timer_thread, NULL, timer, NULL);   

    gtk_main ();
    gdk_threads_leave();
    return 0;
}

我尝试这样做,timer() 正确生成,但主窗口从未出现:

    static void timer( GtkWidget *widget, gpointer data )
    {
        while(1)
        {
            g_print("something"); //for checking
            // some code here to calculate time
            gtk_label_set_markup ((GtkLabel*)label, time_c);
            sleep(1);
        }
    }

int main(int argc, char **argv)
{
    g_signal_connect (window, "show", G_CALLBACK (timer), NULL);
}

提前致谢!

I'm writing a countodown timer. It consists of a single windows with a label in it, where the time is displayed. The "timer()" (in the code below) is spawned as a thread. This works perfectly in a linux environment, but as far as I'm concerned GTK does not support multithreading under windows (correct me if I am wrong). Is it possible to spawn the "timer()" without using threads, e.g. by calling a signal when the main_window appears?

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <gtk/gtk.h>

GtkWidget *window;
GtkWidget *label;
pthread_t timer_thread;

void* timer()
{
    while(1)
    {
        // some code here to calculate time
        gtk_label_set_markup ((GtkLabel*)label, time_c);
        sleep(1);
    }
}

static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data )
{
    return FALSE;
}

static void destroy( GtkWidget *widget, gpointer data )
{
    gtk_main_quit ();
}

int main(int argc, char **argv)
{
    g_thread_init(NULL);
    gdk_threads_init();  
    gdk_threads_enter();
    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    g_signal_connect (window, "delete-event", G_CALLBACK (delete_event), NULL);
    g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);

    label = gtk_label_new("");

    gtk_container_add (GTK_CONTAINER (window), label);
    gtk_widget_show (label);
    gtk_widget_show (window);

    pthread_create(&timer_thread, NULL, timer, NULL);   

    gtk_main ();
    gdk_threads_leave();
    return 0;
}

I tried doing this, the timer() spawned correctly, but the main window never appeared:

    static void timer( GtkWidget *widget, gpointer data )
    {
        while(1)
        {
            g_print("something"); //for checking
            // some code here to calculate time
            gtk_label_set_markup ((GtkLabel*)label, time_c);
            sleep(1);
        }
    }

int main(int argc, char **argv)
{
    g_signal_connect (window, "show", G_CALLBACK (timer), NULL);
}

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

(り薆情海 2024-11-07 16:20:36

为此,您最好使用计时器。查看 g_timeout_add< /代码>。这里有一个示例:GTK+ 事件和信号

You would be better off using a timer for that. Look into g_timeout_add. There's an example here: GTK+ events and signals.

暗藏城府 2024-11-07 16:20:36

使用 g_timeout_addGTimer。初始化您的 GTimer,并在超时回调时调用 g_timer_elapsed< /a>.仅依赖调用 g_timeout_add 时使用的时间会导致计时不准确。

Use g_timeout_add and a GTimer. Initalize your GTimer, and on your timeout callback, call g_timer_elapsed. Relying only on the time used when calling g_timeout_add would cause timing inacurracy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文