php-gtk2怎么用,PHP+GTK2 初体验,简单计算器客户端

1 <?php2

3 class Calculator extendsGtkWindow {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 functiondraw() {17 $this->set_default_size(300, 400);18 $this->set_title("this is my calculator");19

20 //加元素

21 $mesg = new GtkLabel(‘‘);22 $this->chalkboard = newGtkLabel();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(‘

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 functionshow() {103 $this->show_all();104 }105

106

107 //加减乘除

108 public functionplus(){109 $this->operate(‘1‘);110 }111 public functionreduce(){112 $this->operate(‘2‘);113 }114 public functionride(){115 $this->operate(‘3‘);116 }117 public functiondivide(){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 functionchange0(){131 $this->changeParam(‘0‘);132 }133 public functionchange1(){134 $this->changeParam(‘1‘);135 }136 public functionchange2(){137 $this->changeParam(‘2‘);138 }139 public functionchange3(){140 $this->changeParam(‘3‘);141 }142 public functionchange4(){143 $this->changeParam(‘4‘);144 }145 public functionchange5(){146 $this->changeParam(‘5‘);147 }148 public functionchange6(){149 $this->changeParam(‘6‘);150 }151 public functionchange7(){152 $this->changeParam(‘7‘);153 }154 public functionchange8(){155 $this->changeParam(‘8‘);156 }157 public functionchange9(){158 $this->changeParam(‘9‘);159 }160 public functionchangePoint(){161 $this->changeParam(‘.‘);162 }163 public functionchangeMinus(){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 functionparamDle(){178 $this->param = substr($this->param, 0, strlen($this->param)-1);179 $this->notice($this->param);180 }181

182

183 //清存

184 public functionallClear(){185 $this->param = false;186 $this->operater = false;187 $this->tmpres = false;188 $this->notice(‘0‘);189 }190

191 //清屏

192 public functionnowClear(){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 functioncalculate() {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 newCalculator();285 Gtk::main();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值