Php7 GUI development example

Time:2021-6-13

This paper introduces the development of php7 GUI with an example. The details are as follows:

1、 Download specified system extensions


http://pecl.php.net/package/ui
http://pecl.php.net/package/ui/2.0.0/windows

Because my system is 64 bit win10 and the PHP version is 7.1, I choose 7.1 thread safe (TS) x64. Linux users can download the source code package, compile and install it.

Decompress PHP_ Ui-2.0.0-7.1-ts-vc14-x64.zip_ Copy ui.dll to your php.ini extension_ Dir, copy libui.dll and pthreadvc2.dll to the directory of C: / / Windows / system32.

Add the following in php.ini:


extension=php_ui.dll

Then restart the service, check with phpinfo(), and the UI extension is installed successfully.

2、 We create a simple calculator

<?php
use UI\Window;
use UI\Size;
use UI\Controls\Entry;
use UI\Controls\Label;
use UI\Controls\Button;
use UI\Controls\Grid;

//Create a window
//Parameter one represents the window title
//The second parameter is the window size
//Parameter 3 indicates whether the menu is displayed
$win = new window ('simple calculator ', new size (640, 480), true';

//Create a text box
//Entry:: normal normal single line text box
//Entry:: password box
//Entry:: search search box
$num1 = new Entry(Entry::Normal);
$num2 = new Entry(Entry::Normal);
$ret = new Entry(Entry::Normal);

//Create a label
$lab = new Label('+');

//Create a button
$BTN = new class ('compute ') extends button{
  public function __construct($text) {
    parent::__construct($text);
  }

  protected function onClick() {
    global $num1, $num2, $ret;
    $n1 = $num1->getText();
    $n2 = $num2->getText();
    $ret->setText($n1 + $n2);
  }
};

//Create a grid
$grid = new Grid();

//Add the above controls to the grid layout
$grid->append($num1, 0, 0, 1, 1, true, Grid::Center, false, Grid::Center);
$grid->append($lab, 0, 1, 1, 1, true, Grid::Center, false, Grid::Center);
$grid->append($num2, 0, 2, 1, 1, true, Grid::Center, false, Grid::Center);
$grid->append($btn, 0, 3, 1, 1, true, Grid::Center, false, Grid::Center);
$grid->append($ret, 0, 4, 1, 1, true, Grid::Center, false, Grid::Center);

//Add layout to window
$win->add($grid);

//Display window
$win->show();

UI\run();

Running PHP script under the console


> php index.php

The results are as follows

3、 We create a common registration form

<?php
use UI\Window;
use UI\Size;
use UI\Controls\Box;
use UI\Controls\Entry;
use UI\Controls\Button;
use UI\Controls\Form;
use UI\Controls\Radio;
use UI\Controls\Check;
use UI\Controls\Combo;
use UI\Controls\MultilineEntry;

//Create a window
//Parameter one represents the window title
//The second parameter is the window size
//Parameter 3 indicates whether the menu is displayed
$win = new window ('simple calculator ', new size (640, 480), true';

//Create a form
$frm = new Form();

//User name
$user = new Entry(Entry::Normal);
$frm - > append ('user Name: ', $user, false');

//Password
$pwd = new Entry(Entry::Password);
$frm - > append ('password: ', $PWD, false');

//Gender (single choice)
$sex = new Radio();
$sex - > append ("male");
$sex - > append ("female");
$sex - > append ("unknown");
$frm - > append ('gender: ', $sex, false');

//Hobbies (multiple choice)
//Create a box and arrange it horizontally
$loveBox = new Box(Box::Horizontal);
$love1 = new check ('reading ');
$love2 = new check ('travel ');
$love3 = new check ('game ');
//Add multiple selection control to the box
$loveBox->append($love1);
$loveBox->append($love2);
$loveBox->append($love3);
$frm - > append ('hobby: ', $Lovebox, false');

//Region (drop down box)
$area = new Combo();
$area > append ("Beijing");
$area > append ("Shanghai");
$area > append ("Wuhan");
$frm > append ('region: ', $area, false');

//Introduction (multiline text)
$desc = new MultilineEntry();
$frm - > append ('introduction: ', $DESC, false');

//Register button
$reg = new class ('register ', $win) extensions button{
  protected $win;

  public function __construct($text, $win) {
    $this->win = $win;
    parent::__construct($text);
  }

  //The click event used to override the parent class
  protected function onClick() {
    $this - > win - > MSG ('message ',' you click the register button ');
  }
};
$frm->append('', $reg);

$win->add($frm);

//Display window
$win->show();

UI\run();

The results are as follows

In order to avoid running PHP scripts with commands every time, we can write a bat script to run it.


D:\amp\php7\php.exe -f D:\wwwroot\demo1\index.php
pause

Note the path to php.exe and the path to run the script. Save it as run.bat. The running results are as follows:

For more information about PHP, readers interested in it can see the following topics: PHP extension development tutorial, PHP network programming skills summary, PHP curl usage summary, PHP array operation skills encyclopedia, PHP data structure and algorithm tutorial, PHP programming algorithm summary and PHP string Usage Summary

I hope this article is helpful for PHP programming.