gtk为何要用C语言
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gtk为何要用C语言相关的知识,希望对你有一定的参考价值。
我以前准备学GTK的,但是一看到GTK那复杂的结构和别扭的面向对象机制模拟和繁杂的代码我就不想学了,GTK相对其他GUI库来说,结构非常复杂但功能却远远不如它们,用C语言写的却要模拟面向对象机制,结果搞得结构更加复杂,C语言里没有类和封装的概念,所有函数都是相互公开可以互相调用,这样的话直接用回调函数和函数调用就行了,用得着信号/槽机制吗,可是GTK却作了这些多余的工作,让GTK更加复杂,现在的GTK,说是基于C,其实都是用的自己模拟的面向对象机制,C++已经有面向对象机制了,直接用C++就行了,何必要用C再自己模拟,吃饱了撑得慌阿
gtkmm,根本就不像C++的库,而且还会增加额外依赖
GTK+(GIMP Toolkit)是一套跨多种平台的图形工具包,按LGPL许可协议发布的。虽然最初是为GIMP写的,但目前已发展为一个功能强大、设计灵活的一个通用图形库。
GTK+虽然是用C语言写的,但是可以使用熟悉的语言来使用GTK+,因为GTK+已经被绑定到几乎所有流行的语言上,如:C++,php, Guile,Perl, Python, TOM, Ada95, Objective C, Free Pascal, and Eiffel。 参考技术A gtk现在有很多语言的 并不单单是C而已 刚开始用C是因为很多程序员喜欢用C 而已C的效率高啊 兄弟 参考技术B C效率高。
对于像我这种没学过Java,也没深入研究过C++的人来说,不觉得C的面向对象机制复杂和别扭。
C里面虽然没有类和封装的概念,但是有文件的概念,有它在,可以做到函数并不相互“公开”。不同文件中的同名函数,可以指向不同的函数,完全在控制之中。 参考技术C 只是 你太浮躁拉,你没有慢慢的学习,坚持下来,你就看见GTK+是那么简单`!~!其实GTK+的架构挺好的,也没有像你说的繁杂~!
gtk_
开头就是
gdk 这个开头就是这用,
window代表的就是窗口,没有什么不好学习,很明了`!
C语言也能实现面向对象,用C还是c++那要看场合了`! 参考技术D 关于信号/槽机制:这是为了便于增加新的信号,增加新的信号比增加一个新的回调更方便。
用 c 的话可以很容易映射到其它语言上,c++ 则很麻烦。
LZ建议了解下 gdk, gtk+, gtkmm. gtk/gtk+ 和 glib 是一条线上的,而 gtkmm 是完全基于 c++ 的。可以比如说建立 gtk -> ruby 的映射,而 gtkmm 则不能。
PHP+GTK2 初体验,简单计算器客户端
最近工作中要用php重做一个之前用.net实现的工具客户端。准备用php+gtk做,于是查阅资料先做个小的demo熟悉下。
gtk这个东西资料太少了,这里先附上几个链接:
1.gtk的文档(吐槽下很简陋):http://gtk.php.net/manual/en/html/gtk/gtk.gtkwindow.html
2.gtk布局 :http://www.laruence.com/2009/05/26/871.html
3.鸟哥的demo:http://www.laruence.com/2009/05/26/871.html
好了,附上自己的代码
1 <?php 2 3 class Calculator extends GtkWindow { 4 private $chalkboard; //结果显示占位 5 private $param = false; //输入参数 6 private $operater; //运算规则 7 private $tmpres = false; //临时结果 8 9 function __construct() { 10 parent::__construct(); 11 $this->draw(); 12 $this->show(); 13 } 14 15 //UI布局 16 public function draw() { 17 $this->set_default_size(300, 400); 18 $this->set_title("this is my calculator"); 19 20 //加元素 21 $mesg = new GtkLabel(\'\'); 22 $this->chalkboard = new GtkLabel(); 23 24 $btn_1 = new GtkButton(\'1\'); 25 $btn_2 = new GtkButton(\'2\'); 26 $btn_3 = new GtkButton(\'3\'); 27 $btn_4 = new GtkButton(\'4\'); 28 $btn_5 = new GtkButton(\'5\'); 29 $btn_6 = new GtkButton(\'6\'); 30 $btn_7 = new GtkButton(\'7\'); 31 $btn_8 = new GtkButton(\'8\'); 32 $btn_9 = new GtkButton(\'9\'); 33 $btn_0 = new GtkButton(\'0\'); 34 $btn_minus = new GtkButton(\'\\-\'); 35 $btnPlus = new GtkButton(\'+\'); 36 $btnReduce = new GtkButton(\'-\'); 37 $btnRide = new GtkButton(\'*\'); 38 $btnDivide = new GtkButton(\'/\'); 39 $btnGet = new GtkButton(\'=\'); 40 $btnDle = new GtkButton(\'<-\'); 41 $btnAllclear = new GtkButton(\'Clean\'); 42 $btnNowclear = new GtkButton(\'Clear\'); 43 $btnPoint = new GtkButton(\'.\'); 44 45 46 //table布局UI 47 $tbl = new GtkTable(4, 5); 48 $tbl->attach($btnAllclear, 0, 1, 0, 1); 49 $tbl->attach($btnNowclear, 1, 2, 0, 1); 50 $tbl->attach($btnDle, 2, 3, 0, 1); 51 $tbl->attach($btnDivide, 3, 4, 0, 1); 52 $tbl->attach($btn_7, 0, 1, 1, 2); 53 $tbl->attach($btn_8, 1, 2, 1, 2); 54 $tbl->attach($btn_9, 2, 3, 1, 2); 55 $tbl->attach($btnRide, 3, 4, 1, 2); 56 $tbl->attach($btn_4, 0, 1, 2, 3); 57 $tbl->attach($btn_5, 1, 2, 2, 3); 58 $tbl->attach($btn_6, 2, 3, 2, 3); 59 $tbl->attach($btnReduce, 3, 4, 2, 3); 60 $tbl->attach($btn_1, 0, 1, 3, 4); 61 $tbl->attach($btn_2, 1, 2, 3, 4); 62 $tbl->attach($btn_3, 2, 3, 3, 4); 63 $tbl->attach($btnPlus, 3, 4, 3, 4); 64 $tbl->attach($btn_minus, 0, 1, 4, 5); 65 $tbl->attach($btn_0, 1, 2, 4, 5); 66 $tbl->attach($btnPoint, 2, 3, 4, 5); 67 $tbl->attach($btnGet, 3, 4, 4, 5); 68 69 70 //放入组件 71 $vBox = new GtkVBox(false, true); 72 $vBox->pack_start($mesg); 73 $vBox->pack_start($this->chalkboard); 74 $vBox->pack_start($tbl); 75 76 77 //给按键绑定触发事件 78 $btnGet->connect("clicked", array($this, "calculate")); 79 $btnPlus->connect("clicked", array($this, "plus")); 80 $btnReduce->connect("clicked", array($this, "reduce")); 81 $btnRide->connect("clicked", array($this, "ride")); 82 $btnDivide->connect("clicked", array($this, "divide")); 83 $btn_1->connect("clicked", array($this, "change1")); 84 $btn_2->connect("clicked", array($this, "change2")); 85 $btn_3->connect("clicked", array($this, "change3")); 86 $btn_4->connect("clicked", array($this, "change4")); 87 $btn_5->connect("clicked", array($this, "change5")); 88 $btn_6->connect("clicked", array($this, "change6")); 89 $btn_7->connect("clicked", array($this, "change7")); 90 $btn_8->connect("clicked", array($this, "change8")); 91 $btn_9->connect("clicked", array($this, "change9")); 92 $btn_0->connect("clicked", array($this, "change0")); 93 $btn_minus->connect("clicked", array($this, "changeMinus")); 94 $btnPoint->connect("clicked", array($this, "changePoint")); 95 $btnDle->connect("clicked", array($this, "paramDle")); 96 $btnAllclear->connect("clicked", array($this, "allClear")); 97 $btnNowclear->connect("clicked", array($this, "nowClear")); 98 99 $this->add($vBox); 100 } 101 //组件显示 102 public function show() { 103 $this->show_all(); 104 } 105 106 107 //加减乘除 108 public function plus(){ 109 $this->operate(\'1\'); 110 } 111 public function reduce(){ 112 $this->operate(\'2\'); 113 } 114 public function ride(){ 115 $this->operate(\'3\'); 116 } 117 public function divide(){ 118 $this->operate(\'4\'); 119 } 120 public function operate($operate) { 121 $this->operater = $operate; 122 if ($this->param) { 123 $this->tmpres = $this->param; 124 $this->param = false; 125 } 126 } 127 128 129 //数字录入 130 public function change0(){ 131 $this->changeParam(\'0\'); 132 } 133 public function change1(){ 134 $this->changeParam(\'1\'); 135 } 136 public function change2(){ 137 $this->changeParam(\'2\'); 138 } 139 public function change3(){ 140 $this->changeParam(\'3\'); 141 } 142 public function change4(){ 143 $this->changeParam(\'4\'); 144 } 145 public function change5(){ 146 $this->changeParam(\'5\'); 147 } 148 public function change6(){ 149 $this->changeParam(\'6\'); 150 } 151 public function change7(){ 152 $this->changeParam(\'7\'); 153 } 154 public function change8(){ 155 $this->changeParam(\'8\'); 156 } 157 public function change9(){ 158 $this->changeParam(\'9\'); 159 } 160 public function changePoint(){ 161 $this->changeParam(\'.\'); 162 } 163 public function changeMinus(){ 164 $this->changeParam(\'-\'); 165 } 166 public function changeParam($param){ 167 if ($this->param === false) { 168 $this->param = \'\'; 169 } 170 $this->param.= $param; 171 $this->param = $this->fotmat($this->param); 172 $this->notice($this->param); 173 } 174 175 176 177 public function paramDle(){ 178 $this->param = substr($this->param, 0, strlen($this->param)-1); 179 $this->notice($this->param); 180 } 181 182 183 //清存 184 public function allClear(){ 185 $this->param = false; 186 $this->operater = false; 187 $this->tmpres = false; 188 $this->notice(\'0\'); 189 } 190 191 //清屏 192 public function nowClear(){ 193 $this->param = false; 194 $this->notice(\'0\'); 195 } 196 197 //显示结果 198 private function notice($mesg) { 199 $this->chalkboard->set_text($mesg); 200 } 201 202 //计算 203 public function calculate() { 204 $res = $this->getRes($this->tmpres, $this->param, $this->operater); 205 $this->operater = false; 206 $this->param = false; 207 $this->notice($res); 208 return; 209 } 210 211 //运算函数 212 public function getRes($tmpres, $param, $operater) { 213 if ($tmpres!== false || !$operater) { 214 if ($param !== false) { 215 $this->tmpres = $param; 216 } 217 return $this->tmpres; 218 } 219 if ((!$tmpres || $tmpres === false) && $param!== false && $operater) { 220 $this->param = false; 221 if ($operater === \'1\') { 222 $this->tmpres = $tmpres + $param; 223 return $this->tmpres; 224 } 225 if ($operater === \'2\') { 226 $this->tmpres = $tmpres - $param; 227 return $this->tmpres; 228 } 229 if ($operater === \'3\') { 230 $this->tmpres = $tmpres * $param; 231 return $this->tmpres; 232 } 233 if ($operater === \'4\') { 234 if (abs($param) > 0) { 235 $this->tmpres = $tmpres / $param; 236 return $this->tmpres; 237 } else { 238 $this->param = false; 239 $this->operater = false; 240 $this->tmpres = false; 241 return "The divisor cannot be 0"; 242 } 243 } 244 } 245 } 246 247 248 249 //数字处理函数 250 public function fotmat($shownum){ 251 $flag = false; 252 $flag2 = false; 253 $tmpRight = \'\'; 254 if (strstr($shownum, \'-\')) { 255 $shownum = str_replace(\'-\', \'\', $shownum); 256 $flag2 = true; 257 } 258 if (strstr($shownum, \'.\')) { 259 $tmpArr = explode(\'.\', $shownum); 260 $shownum = $tmpArr[0]; 261 $tmpRight = $tmpArr[1]; 262 $flag = true; 263 } 264 if (preg_match_all(\'/([0]{1,})([1-9]*)/\', $shownum, $out)) { 265 if ($out[2][0] === \'\') { 266 $shownum = \'0\'; 267 } else { 268 $shownum = $out[2][0]; 269 } 270 } 271 if ($flag) { 272 $shownum = $shownum.\'.\'.$tmpRight; 273 } 274 if ($flag2) { 275 $shownum = \'-\'.$shownum; 276 } 277 return $shownum; 278 } 279 function __destruct() { 280 Gtk::main_quit(); 281 } 282 } 283 284 new Calculator(); 285 Gtk::main();
弄好之后起来的效果:
先这样,以后有时间重写
以上是关于gtk为何要用C语言的主要内容,如果未能解决你的问题,请参考以下文章