|
#include <gtk/gtk.h>
#include <time.h>
GtkWidget *clocker;
void clock_begin()
{
time_t now;
struct tm *l_time;
gchar buf[100];
now = time( (time_t *) NULL);
l_time = localtime (&now );
sprintf(buf , "%d : %d : %d",l_time->tm_hour,l_time->tm_min,l_time->tm_sec);
gtk_label_set_text(GTK_LABEL(clocker),buf);
}
int main(int argc , char **argv)
{
GtkWidget *window,*hbox,*label;
gtk_init(&argc,&argv);
window = gtk_window_new ( GTK_WINDOW_TOPLEVEL);
gtk_window_set_title ( GTK_WINDOW(window),"时钟");
gtk_window_set_position ( GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size( GTK_WINDOW(window),200,60 );
gtk_container_set_border_width(GTK_CONTAINER(window),10);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
hbox = gtk_hbox_new(FALSE ,0);
gtk_container_add (GTK_CONTAINER(window),hbox);
label = gtk_label_new("现在时刻:");
gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5);
clocker = gtk_label_new(NULL);
gtk_box_pack_start(GTK_BOX(hbox),clocker,FALSE ,FALSE ,5);
gtk_timeout_add(1000,(GtkFunction)clock_begin,NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
} |
|