C GTK+启动时的事件信号
我正在写一个倒计时器。它由一个带有标签的窗口组成,其中显示时间。 “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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您最好使用计时器。查看
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.使用
g_timeout_add
和GTimer
。初始化您的 GTimer,并在超时回调时调用 g_timer_elapsed< /a>.仅依赖调用 g_timeout_add 时使用的时间会导致计时不准确。Use
g_timeout_add
and aGTimer
. 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.