分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

PHP+GTK2 初体验,简单计算器客户端

发布时间:2023-09-06 01:48责任编辑:赖小花关键词:PHP

  最近工作中要用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 初体验,简单计算器客户端

原文地址:https://www.cnblogs.com/jwcrxs/p/8718572.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved