일정한 간격으로 시간 초과 기능을 호출하는 g_timeout_add
또는 g_timeout_add_seconds
을 사용할 수 있습니다. 이 타임 아웃 기능은 이벤트 처리로 인해 지연 될 수 있으므로 정확한 타이밍에 의존해서는 안됩니다. 메인 루프에 대한 자세한 내용은 this developer page을 확인하십시오.
다음은 시작하기위한 예입니다 (이 문제를 즉시 해결할 수 있음). 이 예에서 시간 제한은 초 단위로 설정되므로 g_timeout_add_seconds
을 사용하고 밀리 초 정밀도가 필요한 경우 g_timeout_add
을 사용하십시오.
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
/* Determines if to continue the timer or not */
static gboolean continue_timer = FALSE;
/* Determines if the timer has started */
static gboolean start_timer = FALSE;
/* Display seconds expired */
static int sec_expired = 0;
static void
_quit_cb (GtkWidget *button, gpointer data)
{
(void)button; (void)data; /*Avoid compiler warnings*/
gtk_main_quit();
return;
}
static gboolean
_label_update(gpointer data)
{
GtkLabel *label = (GtkLabel*)data;
char buf[256];
memset(&buf, 0x0, 256);
snprintf(buf, 255, "Time elapsed: %d secs", ++sec_expired);
gtk_label_set_label(label, buf);
return continue_timer;
}
static void
_start_timer (GtkWidget *button, gpointer data)
{
(void)button;/*Avoid compiler warnings*/
GtkWidget *label = data;
if(!start_timer)
{
g_timeout_add_seconds(1, _label_update, label);
start_timer = TRUE;
continue_timer = TRUE;
}
}
static void
_pause_resume_timer (GtkWidget *button, gpointer data)
{
(void)button;/*Avoid compiler warnings*/
if(start_timer)
{
GtkWidget *label = data;
continue_timer = !continue_timer;
if(continue_timer)
{
g_timeout_add_seconds(1, _label_update, label);
}
else
{
/*Decrementing because timer will be hit one more time before expiring*/
sec_expired--;
}
}
}
static void
_reset_timer (GtkWidget *button, gpointer data)
{
(void)button; (void)data;/*Avoid compiler warnings*/
/*Setting to -1 instead of 0, because timer will be triggered once more before expiring*/
sec_expired = -1;
continue_timer = FALSE;
start_timer = FALSE;
}
int main(void)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *start_button;
GtkWidget *pause_resume_button;
GtkWidget *reset_button;
GtkWidget *quit_button;
GtkWidget *label;
gtk_init(NULL, NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit),
NULL);
vbox = gtk_vbox_new (FALSE, 2);
gtk_container_add(GTK_CONTAINER(window), vbox);
label = gtk_label_new("Time elapsed: 0 secs");
start_button = gtk_button_new_with_label("Start");
g_signal_connect(G_OBJECT(start_button), "clicked", G_CALLBACK(_start_timer), label);
pause_resume_button = gtk_button_new_with_label("Pause/Resume");
g_signal_connect(G_OBJECT(pause_resume_button), "clicked", G_CALLBACK(_pause_resume_timer), label);
reset_button = gtk_button_new_with_label("Reset");
g_signal_connect(G_OBJECT(reset_button), "clicked", G_CALLBACK(_reset_timer), label);
quit_button = gtk_button_new_with_label("Quit");
g_signal_connect(G_OBJECT(quit_button), "clicked", G_CALLBACK(_quit_cb), NULL);
gtk_box_pack_start (GTK_BOX(vbox), label, 0, 0, 0);
gtk_box_pack_start (GTK_BOX(vbox), start_button, 0, 0, 0);
gtk_box_pack_start (GTK_BOX(vbox), pause_resume_button, 0, 0, 0);
gtk_box_pack_start (GTK_BOX(vbox), reset_button, 0, 0, 0);
gtk_box_pack_start (GTK_BOX (vbox), quit_button, 0, 0, 0);
gtk_widget_show_all(window);
g_timeout_add_seconds(1, _label_update, label);
continue_timer = TRUE;
start_timer = TRUE;
gtk_main();
return 0;
}
희망이 있습니다.
거기에있는 몇 가지 무서운 코드입니다. 'g_timer_elapsed'는 gdouble을 리턴하고 strcpy에서 그것을 사용하는 것은 좋은 생각이 아닙니다! gtk_frame의 타이머는 의미가 없습니다.) ... 요구 사항은 무엇입니까? 일정한 간격으로 요소를 업데이트 하시겠습니까? –
@ another.anon.coward 타이머가있는 프레임을 원합니다. 지금까지 타이머에서 찾은 유일한 방법은 GTimer입니다. 덜 무서운 코드로이 작업을 수행 할 수있는 또 다른 방법을 알고 있습니까? Hehehe – drodri420
물론 있습니다 :)) ... 시간을 표시 하시겠습니까? 아니면 시간이 경과 했습니까? –