gtk 计时器

一剑行天下 posted @ 2008年11月01日 19:23 in gtk编程 , 2623 阅读

#include<gtk/gtk.h>
#include<time.h>
static  GtkWidget *entry,*entry1;
 static  GtkWidget *button1; 
static GtkWidget *button2;
gint j;


int tttt;
gboolean  time_begin ()
{
        int l=0;
        gchar buf[100];
        static gint sec=0,min=0,hour=0;
        sec++;
        if(sec==60)
        {
                min++;
                if(min==60)
                {
                        hour++;
                        min=0;              
                }               
        sec=0;
        }
        sprintf(buf,"%d:%d:%d",hour,min,sec);
        gtk_entry_set_text(GTK_ENTRY(entry1),buf);
        gtk_widget_show(entry1);

       printf("\nl=%d",l);
       l++;
       return TRUE;
}

gboolean  clocker_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_entry_set_text(GTK_ENTRY(entry),buf);
       g_print("%d", tttt);
       tttt++;
       return TRUE;
}
void  begin (GtkWidget *widget, gpointer data)
{
       gtk_widget_set_sensitive(button1,FALSE);
       gtk_widget_set_sensitive(button2,TRUE);
       j=gtk_timeout_add(1000,(GtkFunction)time_begin,data);
}

void  stop (GtkWidget *widget, gpointer data)
{
       gtk_widget_set_sensitive(button2,FALSE);
       gtk_widget_set_sensitive(button1,TRUE);
       gtk_timeout_remove(j);

}


int main(int argc,char * argv[])
{
        tttt=0;
        GtkWidget *window=NULL;
        GtkWidget *hbox=NULL;
        GtkWidget *box=NULL;
        GtkWidget *h1box,*h2box;
     

        gint i;
        // GtkWidget *scrolledwin=NULL;
        gtk_init(&argc,&argv);
        window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
        g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);

        box=gtk_vbox_new(FALSE,0);
         gtk_container_add(GTK_CONTAINER(window),box);


        hbox=gtk_hbox_new(FALSE,0);
        gtk_box_pack_start(GTK_BOX(box),hbox,FALSE,TRUE,0);

        gtk_widget_show(hbox);
                      entry=gtk_entry_new();
        gtk_box_pack_start(GTK_BOX(hbox),entry,FALSE,TRUE,0);
                i=gtk_timeout_add(1000,(GtkFunction)clocker_begin,NULL);
                h1box=gtk_hbox_new(FALSE,0);// 加计时窗口
                gtk_box_pack_start(GTK_BOX(box),h1box,FALSE,TRUE,0);


                entry1=gtk_entry_new();
                 gtk_box_pack_start(GTK_BOX(h1box),entry1,FALSE,TRUE,0);
                       h2box=gtk_hbox_new(FALSE,0);// 加按钮
                       gtk_box_pack_start(GTK_BOX(box),h2box,FALSE,TRUE,0);


                      
                button1=gtk_button_new_with_label("开始");
                 gtk_box_pack_start(GTK_BOX(h2box),button1,FALSE,TRUE,0);
                 g_signal_connect(G_OBJECT(button1),"clicked",G_CALLBACK(begin),entry1);
                  button2=gtk_button_new_with_label("停止");
                 gtk_box_pack_start(GTK_BOX(h2box),button2,FALSE,TRUE,0);
                  g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(stop),entry1);


                 //gtk_timeout_add(1000,(GtkFunction)time_begin,NULL);


       gtk_widget_show_all(window);
        gtk_main();
        return FALSE;
}

Avatar_small
yyy 说:
2009年3月11日 00:26

这是专门的软件?


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter