| 1 |
|
| 2 |
|
| 3 |
========================= |
| 4 |
SimGUI Manual |
| 5 |
========================= |
| 6 |
|
| 7 |
:Authors: Klaus Muller <[email protected]> |
| 8 |
:SimPy release: |release| |
| 9 |
:Web-site: http://simpy.sourceforge.net/ |
| 10 |
:Python-Version: 2.6 and later |
| 11 |
:Date: December 2011 |
| 12 |
|
| 13 |
|
| 14 |
.. contents:: Contents |
| 15 |
:depth: 2 |
| 16 |
|
| 17 |
This manual describes **SimGUI**, a GUI framework for SimPy simulation applications. |
| 18 |
|
| 19 |
Acknowledgements |
| 20 |
================ |
| 21 |
|
| 22 |
The initial ideas for using a Tk/Tkinter-based GUI for SimPy simulation models |
| 23 |
and first applications came from Mike Mellor and Prof. Simon Frost of University |
| 24 |
of California, San Diego. Simon has been a very productive co-developer of SimGUI. |
| 25 |
|
| 26 |
Following an idea by Simon Frost, SimGUI uses a great Tkinter-based console for conversing |
| 27 |
with the Python interpreter, developed by Ka-Ping Yee <[email protected]>. |
| 28 |
|
| 29 |
Introduction |
| 30 |
============ |
| 31 |
|
| 32 |
SimGUI is a GUI framework for SimPy simulation programs. It provides for: |
| 33 |
|
| 34 |
- a standard layout of the user interface, including its menu structure |
| 35 |
- running a simulation |
| 36 |
- viewing simulation output |
| 37 |
- saving the output to a file |
| 38 |
- changing the simulation model parameters |
| 39 |
- viewing a model description |
| 40 |
- viewing the simulation program's code |
| 41 |
- a Python console for debugging |
| 42 |
|
| 43 |
SimGUI is based on the Tk GUI library. |
| 44 |
|
| 45 |
SimGUI overview |
| 46 |
=============== |
| 47 |
|
| 48 |
Here is a minimalistic program which does nothing but show the SimGUI user interface:: |
| 49 |
|
| 50 |
## SimGUIminimal.py |
| 51 |
from SimPy.SimGUI import * |
| 52 |
root=Tk() |
| 53 |
gu=SimGUI(root,consoleHeight=20) |
| 54 |
gu.mainloop() |
| 55 |
|
| 56 |
Running it produces this output: |
| 57 |
|
| 58 |
.. image:: /_static/images/gui_manual/Minimal.png |
| 59 |
|
| 60 |
The large frame is the **SimGUI application window**. To its right are |
| 61 |
the standard menu items of SimGUI. To show them, the menus have been torn off by |
| 62 |
clicking on the dotted line on all SimGUI drop-down menu items. |
| 63 |
|
| 64 |
The SimGUI application window consists of five widgets: |
| 65 |
|
| 66 |
- the outside frame is a Tk Toplevel widget with a default title (which can be changed), |
| 67 |
|
| 68 |
- a **menu bar**, a Tk Menu widget which can be adapted by the application program (contained in toplevel) |
| 69 |
- the **output window**, a Tk Frame widet for SimPy program output (contained in toplevel) |
| 70 |
|
| 71 |
- the **status bar** for one-line status messages, a Tk Label widget (contained in output window) |
| 72 |
|
| 73 |
- the **output console** for application output, a Tk Text widget (contained in output window) |
| 74 |
|
| 75 |
The **File** sub-menu is for saving or opening files and also for quitting the application. By default, it supports |
| 76 |
saving the content of the output console. |
| 77 |
|
| 78 |
The **Edit** sub-menu is for any editing or change operations. By default, it supports changing model parameters |
| 79 |
and clearing the output console. |
| 80 |
|
| 81 |
The **Run** sub-menu is for running the SimPy model. By default, it is empty, as there is no model to run. |
| 82 |
|
| 83 |
The **View** sub-menu is for calling up any application output, e.g. plots or statistics. By default, it has the ability |
| 84 |
to automatically show the data from any Monitor instance in the SimPy application. |
| 85 |
|
| 86 |
The **Help** sub-menu is for viewing any information about SimGUI and the specific application behind it. |
| 87 |
By default, it provides information about the version of SimGUI, lists the application's code and allows |
| 88 |
the launching of a Python console for testing and debugging purposes. |
| 89 |
|
| 90 |
To show how simple it is to interface a model to SimGUI, here is an example with a simple simulation model:: |
| 91 |
|
| 92 |
#!/usr/bin/env python |
| 93 |
__doc__=""" GUIdemo.py |
| 94 |
This is a very basic model, demonstrating the ease |
| 95 |
of interfacing to SimGUI. |
| 96 |
""" |
| 97 |
from __future__ import generators |
| 98 |
from SimPy.Simulation import * |
| 99 |
from random import * |
| 100 |
from SimPy.SimGUI import * |
| 101 |
|
| 102 |
class Launcher(Process): |
| 103 |
nrLaunched=0 |
| 104 |
def launch(self): |
| 105 |
while True: |
| 106 |
gui.writeConsole("Launch at %.1f"%now()) |
| 107 |
Launcher.nrLaunched+=1 |
| 108 |
gui.launchmonitor.observe(Launcher.nrLaunched) |
| 109 |
yield hold,self,uniform(1,gui.params.maxFlightTime) |
| 110 |
gui.writeConsole("Boom!!! Aaaah!! at %.1f"%now()) |
| 111 |
|
| 112 |
def model(): |
| 113 |
gui.launchmonitor=Monitor(name="Rocket counter", |
| 114 |
ylab="nr launched",tlab="time") |
| 115 |
initialize() |
| 116 |
Launcher.nrLaunched=0 |
| 117 |
for i in range(gui.params.nrLaunchers): |
| 118 |
lau=Launcher() |
| 119 |
activate(lau,lau.launch()) |
| 120 |
simulate(until=gui.params.duration) |
| 121 |
gui.noRunYet=False |
| 122 |
gui.writeStatusLine("%s rockets launched in %.1f minutes"% |
| 123 |
(Launcher.nrLaunched,now())) |
| 124 |
|
| 125 |
class MyGUI(SimGUI): |
| 126 |
def __init__(self,win,**par): |
| 127 |
SimGUI.__init__(self,win,**par) |
| 128 |
self.run.add_command(label="Start fireworks", |
| 129 |
command=model,underline=0) |
| 130 |
self.params=Parameters(duration=2000,maxFlightTime=11.7,nrLaunchers=3) |
| 131 |
|
| 132 |
root=Tk() |
| 133 |
gui=MyGUI(root,title="RocketGUI",doc=__doc__,consoleHeight=40) |
| 134 |
gui.mainloop() |
| 135 |
|
| 136 |
|
| 137 |
It produces the following output when the model run command is selected: |
| 138 |
|
| 139 |
.. image:: /_static/images/gui_manual/GUIdemo.png |
| 140 |
|
| 141 |
class MyGUI adds one menu item under the Run menu. It also defines three parameters |
| 142 |
(duration, maxFlighTime and nrLaunchers) with their initial values which the user can |
| 143 |
change interactively before each run. The MyGUI instance named gui sets the |
| 144 |
window title, the model description (the __doc__ string) and the height of the |
| 145 |
output window. |
| 146 |
|
| 147 |
The simulation part of the program writes to the output console and to the status line. |
| 148 |
|
| 149 |
The model uses a Monitor for keeping track of the number of rockets launched over |
| 150 |
time. Because it is made an attribute of the MyGUI instance, the super class |
| 151 |
(SimGUI) can output the Monitor after a run. This requires no application code. |
| 152 |
When the menu item 'Collected data' under the View menu is selected, this |
| 153 |
results in: |
| 154 |
|
| 155 |
.. image:: /_static/images/gui_manual/GUIdemoMoni.png |
| 156 |
|
| 157 |
The SimGUI API |
| 158 |
=============== |
| 159 |
|
| 160 |
Structure |
| 161 |
--------- |
| 162 |
The SimGUI module exposes the following API to the applications programmer:: |
| 163 |
|
| 164 |
class SimGUI(object) |
| 165 |
def __init__ |
| 166 |
self.doc = doc |
| 167 |
self.noRunYet=True |
| 168 |
self.top = Menu |
| 169 |
self.file = Menu |
| 170 |
self.edit = Menu |
| 171 |
self.run = Menu |
| 172 |
self.view = Menu |
| 173 |
self.help = Menu |
| 174 |
def makeFileMenu |
| 175 |
def makeEditMenu |
| 176 |
def makeRunMenu |
| 177 |
def makeViewMenu |
| 178 |
def makeHelpMenu |
| 179 |
def writeConsole |
| 180 |
def saveConsole |
| 181 |
def clearConsole |
| 182 |
def writeStatusLine |
| 183 |
def about |
| 184 |
def notdone |
| 185 |
def showTextBox |
| 186 |
def mainloop |
| 187 |
|
| 188 |
class Parameters |
| 189 |
def __init__ |
| 190 |
def show |
| 191 |
|
| 192 |
|
| 193 |
**class SimGUI, __init__ constructor** |
| 194 |
-------------------------------------- |
| 195 |
Encapsulates the SimGUI functionality. |
| 196 |
|
| 197 |
Call: |
| 198 |
**<variable>.SimGUI(win[,optional parameters])** |
| 199 |
|
| 200 |
Mandatory parameters: |
| 201 |
- **win** : the master widget in which the SimGUI widgets are embedded |
| 202 |
|
| 203 |
Optional parameters: |
| 204 |
- **title=SimGUI** : the title of the top level window (string) |
| 205 |
- **doc=None** : the model description (string) |
| 206 |
- **consoleHeight=50** : the height of the output console in lines (positive integer) |
| 207 |
|
| 208 |
Return value: |
| 209 |
Returns a reference to the SimGUI instance. |
| 210 |
|
| 211 |
**SimGUI instance fields** |
| 212 |
--------------------------------------------------- |
| 213 |
In addition to the constructor parameters, the SimGUI fields of interest |
| 214 |
to the applications programmer are: |
| 215 |
|
| 216 |
- **self.noRunYet=True** : a predicate indicating whether the model has been run yet; must be set to True after each model run; should be tested by application program before any run-dependent output is produced (boolean) |
| 217 |
- **self.top = Menu** : the top level menu widget (menu bar) |
| 218 |
- **self.file = Menu** : the 'File' menu widger |
| 219 |
- **self.edit = Menu** : the 'Edit' menu widget |
| 220 |
- **self.run = Menu** : the 'Run' menu widget |
| 221 |
- **self.view = Menu** : the 'View' menu widget |
| 222 |
- **self.help = Menu** : the 'Help' menu widget |
| 223 |
|
| 224 |
**Adding menu items** |
| 225 |
---------------------- |
| 226 |
Menu items can be added to SimGUI submenus by: |
| 227 |
|
| 228 |
**<menufield>.add_command(label= <string,command=<callable>, underline=<integer>)** |
| 229 |
|
| 230 |
E.g. *self.run.add_command(label="Start fireworks",command=model,underline=0)*. This is all |
| 231 |
standard Tk/Tkinter syntax -- read any Tk/Tkinter manual or book. |
| 232 |
|
| 233 |
**Changing menus** |
| 234 |
-------------------- |
| 235 |
Any of the submenus provided by SimGUI can be replaced by overloading one or |
| 236 |
more of the methods **makeFileMenu, makeEditMenu, makeRunMenu, makeViewMenu, |
| 237 |
makeHelpMenu**. This is done by defining the methods to be overloaded in the SimGUI |
| 238 |
subclass. |
| 239 |
|
| 240 |
The overloading method should look like:: |
| 241 |
|
| 242 |
def makeFileMenu(): |
| 243 |
self.file = Menu(self.top) |
| 244 |
self.file.add_command(label='Save some results', |
| 245 |
command=self.saveResults, underline=0) |
| 246 |
self.file.add_command(label='Get out of here', |
| 247 |
command=self.win.quit,underline=0) |
| 248 |
self.top.add_cascade(label='File',menu=self.file,underline=0) |
| 249 |
|
| 250 |
*Note: It is advisable to keep the basic the SimGUI menu structure in order |
| 251 |
to maintain the SimGUI look-and-feel.* |
| 252 |
|
| 253 |
**writeConsole** |
| 254 |
---------------- |
| 255 |
|
| 256 |
Writes a string into the output console Text widget, with newline at end. |
| 257 |
|
| 258 |
Call: |
| 259 |
|
| 260 |
**<SimGUI instance>.writeConsole(text)** |
| 261 |
|
| 262 |
Mandatory parameters: |
| 263 |
None. |
| 264 |
|
| 265 |
Optional parameters: |
| 266 |
- **text=''** : text to write (string) |
| 267 |
|
| 268 |
Return value: |
| 269 |
None. |
| 270 |
|
| 271 |
**saveConsole** |
| 272 |
--------------- |
| 273 |
Saves output console to a text file which the user is prompted to name. |
| 274 |
|
| 275 |
Call: |
| 276 |
**<SimGUI instance>.saveConsole()** |
| 277 |
|
| 278 |
Mandatory parameters: |
| 279 |
None |
| 280 |
|
| 281 |
Optional parameters: |
| 282 |
None |
| 283 |
|
| 284 |
Return value: |
| 285 |
None |
| 286 |
|
| 287 |
**clearConsole** |
| 288 |
---------------- |
| 289 |
Clears output console. |
| 290 |
|
| 291 |
Call: |
| 292 |
**<SimGUI instance>.clearConsole()** |
| 293 |
|
| 294 |
Mandatory parameters: |
| 295 |
None |
| 296 |
|
| 297 |
Optional parameters: |
| 298 |
None |
| 299 |
|
| 300 |
Return value: |
| 301 |
None |
| 302 |
|
| 303 |
**writeStatusLine** |
| 304 |
-------------------- |
| 305 |
Writes a one-line string to the status line. |
| 306 |
|
| 307 |
Call: |
| 308 |
|
| 309 |
**<SimGUI instance>.writeStatusLine(text)** |
| 310 |
|
| 311 |
Mandatory parameters: |
| 312 |
None. |
| 313 |
|
| 314 |
Optional parameters: |
| 315 |
- **text=''** : text to write (string, not longer than 80 character) |
| 316 |
|
| 317 |
Return value: |
| 318 |
None. |
| 319 |
|
| 320 |
**notdone** |
| 321 |
----------- |
| 322 |
|
| 323 |
Brings up a warning box staing that a feature is not implemented yet. |
| 324 |
Useful during development of application. |
| 325 |
|
| 326 |
Call: |
| 327 |
|
| 328 |
**<SimGUI instance>.notdone()** |
| 329 |
|
| 330 |
Mandatory parameters: |
| 331 |
None. |
| 332 |
|
| 333 |
Optional parameters: |
| 334 |
None. |
| 335 |
|
| 336 |
Return value: |
| 337 |
None. |
| 338 |
|
| 339 |
**showTextBox** |
| 340 |
--------------- |
| 341 |
Pops up a text box (Text widget)with a text in it. |
| 342 |
|
| 343 |
Call: |
| 344 |
|
| 345 |
**<SimGUI instance>.showTextBox(optional parameters)** |
| 346 |
|
| 347 |
Mandatory parameters: |
| 348 |
None. |
| 349 |
|
| 350 |
Optional parameters: |
| 351 |
- **title=''** : title of text box (string) |
| 352 |
- **text=''** : text to write (string) |
| 353 |
- **width=80** : width of box in characters (positive integer) |
| 354 |
- **height=10** : height of box in lines (positive integer) |
| 355 |
|
| 356 |
Return value: |
| 357 |
None. |
| 358 |
|
| 359 |
**mainloop** |
| 360 |
------------ |
| 361 |
Starts up SimGUI execution. |
| 362 |
|
| 363 |
Call: |
| 364 |
**<SimGUI instance>.mainloop()** |
| 365 |
|
| 366 |
Mandatory parameters: |
| 367 |
None. |
| 368 |
|
| 369 |
Optional parameters: |
| 370 |
None. |
| 371 |
|
| 372 |
Return value: |
| 373 |
None |
| 374 |
|
| 375 |
**class Parameters, __init__ constructor and adding parameters** |
| 376 |
---------------------------------------------------------------- |
| 377 |
This class provides for interactive user changes of model parameters. Any user-input |
| 378 |
is checked against the type of the original (default) value of the parameter.In this version, |
| 379 |
parameters of type integer, floating point, text and list are supported. |
| 380 |
Boolean parameters can be implemented by using 0 for False and 1 for True. |
| 381 |
|
| 382 |
Example: |
| 383 |
gui.params=Parameters(endtime=2000, |
| 384 |
numberCustomers=50, |
| 385 |
interval=10.0, |
| 386 |
trace=0) |
| 387 |
|
| 388 |
This results in parameters gui.params.numberCustomers, gui.params.interval and gui.params.trace. |
| 389 |
|
| 390 |
Call: |
| 391 |
**<SimGUI instance>.params=Parameters(par)** (constructor) |
| 392 |
**<SimGUI instance>.params.<name>=<value>** (adding parameters) |
| 393 |
|
| 394 |
Mandatory parameters: |
| 395 |
- **par** : one or more pairs *<name>=<value>*, separated by commas. <value> must be one of the four types supported. |
| 396 |
|
| 397 |
- **<name>** : the parameter name (any legal Python identifier) |
| 398 |
|
| 399 |
- **<value>** : the parameter's initial value (must be one of the four types supported, i.e. integer/boolean, floating point, text and list) |
| 400 |
Optional parameters: |
| 401 |
None. |
| 402 |
|
| 403 |
Return value: |
| 404 |
A Parameter instance. |
| 405 |
|
| 406 |
**show** |
| 407 |
-------- |
| 408 |
Returns the parameter name/value pairs of a Parameter instance in pretty-printed form |
| 409 |
(one pair per line). |
| 410 |
|
| 411 |
Call: |
| 412 |
**<Parameter instance>.show()** |
| 413 |
|
| 414 |
Mandatory parameters: |
| 415 |
None. |
| 416 |
|
| 417 |
Optional parameters: |
| 418 |
None. |
| 419 |
|
| 420 |
Return value: |
| 421 |
A string with the name/value pairs separated by newline ('\n'). |
| 422 |
|
| 423 |
Miscellaneous SimGUI capabilities |
| 424 |
================================= |
| 425 |
|
| 426 |
**Source code listing** |
| 427 |
----------------------------- |
| 428 |
The 'Model code' item of the 'Help' submenu lists the application code |
| 429 |
of a running SimGUI application by outputting the content of sys.argv[0]. |
| 430 |
No user programming is required for this. |
| 431 |
|
| 432 |
**Automatic display of Monitor instances** |
| 433 |
---------------------------------------------- |
| 434 |
After a model run, any Monitor instance which is referred to by a SimGUI (sub)class instance |
| 435 |
is shown in the output console by the 'Collected data' item on the 'View' |
| 436 |
submenu. Just e.g. **gui.waitMon=Monitor("Waiting times")**, where gui refers to |
| 437 |
a SimGUI (sub)class instance, is enough to facilitate this. |
| 438 |
|
| 439 |
|
| 440 |
$Revision: 297 $ $Date: 2009-03-31 02:24:46 +1300 (Tue, 31 Mar 2009) $ kgm |