1. 程式人生 > >PHP+GTK2 初體驗,簡單計算器客戶端

PHP+GTK2 初體驗,簡單計算器客戶端

AC BE ble else none alc IE log flag

  最近工作中要用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();
View Code

  弄好之後起來的效果:

  技術分享圖片

  先這樣,以後有時間重寫

  

PHP+GTK2 初體驗,簡單計算器客戶端