-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
executable file
·226 lines (195 loc) · 6.47 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpGui\Application;
use PhpGui\Widget\Window;
use PhpGui\Widget\Label;
use PhpGui\Widget\Button;
use PhpGui\Widget\Input;
use PhpGui\Widget\TopLevel;
use PhpGui\Widget\Menu;
$app = new Application();
// Create main window
$window = new Window([
'title' => 'Hello World Example in php',
'width' => 800,
'height' => 600
]);
// Label Example
$label = new Label($window->getId(), [
'text' => 'Hello, PHP GUI World!'
]);
$label->pack(['pady' => 20]);
// Extra styled Button example
$styledButton = new Button($window->getId(), [
'text' => 'Styled Button',
'command' => function () use ($label) {
echo "Styled Button clicked!\n";
$label->setText('Styled Button clicked!');
},
'bg' => 'blue',
'fg' => 'white',
'font' => 'Helvetica 16 bold'
]);
$styledButton->pack(['pady' => 10]);
// New Input widget example with extra configuration
$input = new Input($window->getId(), [
'text' => 'Type here...',
'bg' => 'lightyellow',
'fg' => 'black',
'font' => 'Arial 14'
]);
$input->pack(['pady' => 10]);
// Register event listener for Enter key on the input widget
$input->onEnter(function () use ($input) {
echo "Input Enter Pressed: " . $input->getValue() . "\n";
});
$styledLabel = new Label($window->getId(), [
'text' => 'This is a styled label with custom colors',
'fg' => 'white',
'bg' => '#4CAF50',
'font' => 'Arial 12',
'padx' => 10,
'pady' => 5,
'relief' => 'raised'
]);
$styledLabel->pack(['pady' => 5]);
// Dynamic Label update example
$dynamicLabel = new Label($window->getId(), [
'text' => 'Dynamic Lable',
'font' => 'Arial 11 italic',
'fg' => '#666666'
]);
$dynamicLabel->pack(['pady' => 5]);
// Button to demonstrate label updates
$updateButton = new Button($window->getId(), [
'text' => 'Update Labels',
'command' => function () use ($dynamicLabel, $styledLabel) {
$dynamicLabel->setText('Label text updated!');
$dynamicLabel->setForeground('#009688');
$styledLabel->setBackground('#2196F3');
$styledLabel->setText('Colors and text can be changed dynamically');
}
]);
$updateButton->pack(['pady' => 5]);
// Menu Examples
$mainMenu = new Menu($window->getId(), ['type' => 'main']);
// File Menu
$fileMenu = $mainMenu->addSubmenu('File');
$fileMenu->addCommand('New', function () use ($dynamicLabel) {
$dynamicLabel->setText('New File Selected');
});
$fileMenu->addCommand('Open', function () use ($dynamicLabel) {
$dynamicLabel->setText('Open Selected');
});
$fileMenu->addSeparator();
$fileMenu->addCommand('Exit', function () {
exit();
}, ['foreground' => 'red']);
// Edit Menu
$editMenu = $mainMenu->addSubmenu('Edit');
$editMenu->addCommand('Copy', function () use ($styledLabel) {
$styledLabel->setText('Copy Selected');
});
$editMenu->addCommand('Paste', function () use ($styledLabel) {
$styledLabel->setText('Paste Selected');
});
// Help Menu with Nested Submenu
$helpMenu = $mainMenu->addSubmenu('Help');
$aboutMenu = $helpMenu->addSubmenu('About');
$aboutMenu->addCommand('Version', function () use ($dynamicLabel) {
$dynamicLabel->setText('Version 1.0');
});
// TopLevel Examples
$topLevelButton = new Button($window->getId(), [
'text' => 'Open New Window',
'command' => function () use ($dynamicLabel) {
$topLevel = new TopLevel([
'title' => 'New Window Example',
'width' => 300,
'height' => 200
]);
// Add content to TopLevel
$label = new Label($topLevel->getId(), [
'text' => 'This is a new window',
'font' => 'Arial 14'
]);
$label->pack(['pady' => 20]);
$closeBtn = new Button($topLevel->getId(), [
'text' => 'Close Window',
'command' => function () use ($topLevel, $dynamicLabel) {
$dynamicLabel->setText('TopLevel window closed');
$topLevel->destroy();
}
]);
$closeBtn->pack(['pady' => 10]);
$minimizeBtn = new Button($topLevel->getId(), [
'text' => 'Minimize',
'command' => function () use ($topLevel) {
$topLevel->iconify();
}
]);
$minimizeBtn->pack(['pady' => 10]);
$dynamicLabel->setText('New window opened');
}
]);
$topLevelButton->pack(['pady' => 10]);
// Dialog Examples
$dialogsLabel = new Label($window->getId(), [
'text' => 'Dialog Examples:',
'font' => 'Arial 12 bold'
]);
$dialogsLabel->pack(['pady' => 5]);
// Color Picker Dialog
$colorButton = new Button($window->getId(), [
'text' => 'Choose Color',
'command' => function () use ($dynamicLabel) {
try {
$color = TopLevel::chooseColor();
if ($color) {
echo "Selected color: $color\n";
$dynamicLabel->setText("Selected color: $color");
$dynamicLabel->setForeground($color);
}
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
}
]);
$colorButton->pack(['pady' => 5]);
// File Selection Dialog
$fileButton = new Button($window->getId(), [
'text' => 'Open File',
'command' => function () use ($dynamicLabel) {
$file = TopLevel::getOpenFile();
if ($file) {
$dynamicLabel->setText("Selected file: " . basename($file));
}
}
]);
$fileButton->pack(['pady' => 5]);
// Directory Selection Dialog
$dirButton = new Button($window->getId(), [
'text' => 'Choose Directory',
'command' => function () use ($dynamicLabel) {
try {
$dir = TopLevel::chooseDirectory();
if ($dir) {
echo "Selected directory: $dir\n";
$dynamicLabel->setText("Selected directory: " . basename($dir));
}
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
}
]);
$dirButton->pack(['pady' => 5]);
// Message Box Example
$msgButton = new Button($window->getId(), [
'text' => 'Show Message',
'command' => function () use ($dynamicLabel) {
$result = TopLevel::messageBox("This is a test message", "okcancel");
$dynamicLabel->setText("Message result: $result");
}
]);
$msgButton->pack(['pady' => 5]);
$app->run();