|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
常常有些朋友在Linux论坛问一些问题,不过,其中大多数的问题都是很基的。
在GTK中,假如您要准时让程序往作某件事,则可使用g_timeout_add()或g_timeout_add_full().一个例子以下:- 这个例子的感化就是把以后工夫显现到窗口中,即显现了一个及时时钟。
复制代码- //~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
复制代码- #include<cairo.h>#include<gtk/gtk.h>#include<time.h>staticcharbuffer[256];
复制代码- /*******************************把buffer显现到窗口中
复制代码- */staticgbooleanon_expose_event(GtkWidget*widget,GdkEventExpose*event,gpointerdata){cairo_t*cr;cr=gdk_cairo_create(widget->window);cairo_move_to(cr,30,30);cairo_show_text(cr,buffer);cairo_destroy(cr);returnFALSE;}
复制代码 [/code]- [/code][code]/*******************************把以后工夫打印到buffer中,而且重画窗口
复制代码- */staticgbooleantime_handler(GtkWidget*widget){if(widget->window==NULL)returnFALSE;time_tcurtime;structtm*loctime;curtime=time(NULL);loctime=localtime(&curtime);strftime(buffer,256,"%T",loctime);gtk_widget_queue_draw(widget);returnTRUE;}
复制代码- //~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~0
复制代码- //~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1
复制代码 [/code][/code][/code]例子
http://zetcode.com/tutorials/gtktutorial/gtkevents/
这两个函数的申明见下:
g_timeout_add()
- //~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2
复制代码 Setsafunctiontobecalledatregularintervals,withthedefaultpriority,G_PRIORITY_DEFAULT.ThefunctioniscalledrepeatedlyuntilitreturnsFALSE,atwhichpointthetimeoutisautomaticallydestroyedandthefunctionwillnotbecalledagain.Thefirstcalltothefunctionwillbeattheendofthefirstinterval.
Notethattimeoutfunctionsmaybedelayed,duetotheprocessingofothereventsources.Thustheyshouldnotbereliedonforprecisetiming.Aftereachcalltothetimeoutfunction,thetimeofthenexttimeoutisrecalculatedbasedonthecurrenttimeandthegiveninterval(itdoesnottrytocatchuptimelostindelays).
interval:thetimebetweencallstothefunction,inmilliseconds(1/1000thsofasecond)function:functiontocalldata:datatopasstofunctionReturns:theID(greaterthan0)oftheeventsource.
第一个参数是距离的毫秒数,第二个参数是准时后的callback,第三个是传送给callback的数据。callback的情势以下:
ginttimeout_callback(gpointerdata);
g_timeout_add的前往值能够用来停止这个timeout,以下(假设前往放到tag中)
voidg_source_remove(ginttag);
也能够让callback前往0或FALSE来停止timeout。
更多的参考可见GTK+tutorial相干章节:
http://library.gnome.org/devel/gtk-tutorial/stable/c1761.html#SEC-TIMEOUTS
g_timeout_add_full()
- //~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~3
复制代码 Setsafunctiontobecalledatregularintervals,withthegivenpriority.ThefunctioniscalledrepeatedlyuntilitreturnsFALSE,atwhichpointthetimeoutisautomaticallydestroyedandthefunctionwillnotbecalledagain.Thenotifyfunctioniscalledwhenthetimeoutisdestroyed.Thefirstcalltothefunctionwillbeattheendofthefirstinterval.
Notethattimeoutfunctionsmaybedelayed,duetotheprocessingofothereventsources.Thustheyshouldnotbereliedonforprecisetiming.Aftereachcalltothetimeoutfunction,thetimeofthenexttimeoutisrecalculatedbasedonthecurrenttimeandthegiveninterval(itdoesnottrytocatchuptimelostindelays).
priority:thepriorityoftheidlesource.TypicallythiswillbeintherangebetweenG_PRIORITY_DEFAULT_IDLEandG_PRIORITY_HIGH_IDLE.interval:thetimebetweencallstothefunction,inmilliseconds(1/1000thsofasecond)function:functiontocalldata:datatopasstofunctionnotify:functiontocallwhentheidleisremoved,orNULLReturns:theID(greaterthan0)oftheeventsource.
学习python,无论你是打算拿他当主要开发语言,还是当辅助开发语言,你都应该学习他,因为有些时间我们耗不起。 |
|