Python Example - Dynamic with GUI

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: User Guide ➔ COM Automation ➔ Python Automation ➔ Example - Dynamic with GUI

Python Setup Python Examples
Installation &
Basic Usage & Scenarios Constrained FEM
(COM | scipy tools)
Model Testing
with GUI
External DLL
Model Generation
Importing data
to SysCAD


Most of the COM commands are the same for both Steady State and Dynamic. However instead of Probal.Run() we have commands like Dynamic.Start() and Dynamic.Stop().

Here is an example of using Tk (the default python GUI) to create a simple control panel for a Dynamic model.


Running this script will create a Tk window with a slider control which can be used to adjust the capacity of the outflow from the tank. Interaction with a Dynamic model where you are opening and closing valves and turning pumps on and off is much easier with a dedicated GUI interface, and this can be done easily with python.

This example shows a number of features of Tk which is the default GUI for python. The variable Capacity is a Tk DoubleVar, which represents a real number but has other useful features.

  • We can attach a callback function to the variable, so when the variable is changed the function is executed. In this case, the callback function sets the SysCAD tag to the (changed) value of Capacity.
  • Capacity is hooked to to the Scale widget as a variable - so when the scale is moved by dragging with the mouse, the value is changed (and the callback to write the SysCAD tag is initiated.
Capacity = DoubleVar(value = 0)

def capCallback(*args):
    sc[ctrlTag] = Capacity.get()

Capacity.trace("w", capCallback) ## The trace method of a DoubleVar executes the callback argument when the variable is written

Scale(root, orient = HORIZONTAL, variable = Capacity).pack(side=TOP)
  • We also use Capacity in the controller method CtrlLevel, to set the value to either 200 (when the level goes over 90%, or 5, when the level drops to 30% (not a very good level control strategy)
  • When this method is called and control is active, it reads the level and changes the Capacity accordingly. The last thing it does is to schedule another call of itsself after 200ms and thus is repeatedly called.
    root.after(200, CtrlLevel) ## Schedule to call the function again after 200ms
Python Code      
## Example Automation Using python for Dynamic model with GUI.

import syscadif
from tkinter import *
ScdDir = r'C:\SysCAD139'
ScdPrj = r'\ExamplesDynamic\Dynamic Plant Example.spf\Project.spj'

sc = syscadif.SysCADCom()  ## Fire up SysCAD
sc.OpenProject(ScdDir+ScdPrj)  ## Open project 
sc["PID_Lvl.Cfg.[1].On"] = 0  ## Turn of level control
ctrlTag = "Output.FC.Qm.Capacity (t/h)"    ## We will do this by hand...

##  Tk Graphical Interface...
root = Tk()
Capacity = DoubleVar(value = 0)
Auto = BooleanVar(value = False)
pid = None

def capCallback(*args):
    sc[ctrlTag] = Capacity.get()

def Stop():

def Done():
    if pid is not None:
        root.after_cancel(pid)   ## Delete scheduled after command

def Ctrl():
    global pid
    if Auto.get():
        lvl = sc["REACTION_TANK.Lvl (%)"]
        if lvl>90:
        elif lvl<30:
    pid = root.after(200, Ctrl)  ## reschedule call in 200ms 
Capacity.trace("w", capCallback) ## The trace method of a DoubleVar executes the callback argument when the variable is written
Label(root, text =  "Capacity Control").pack(side=TOP)
Scale(root, orient = HORIZONTAL, variable = Capacity).pack(side=TOP)
f = Frame(root)
Button(f, text="Run", command =, anchor=NW)
Button(f, text="Stop", command = Stop).pack(side=LEFT,anchor=NW)
Button(f, text="Quit", command = Done).pack(side=LEFT,anchor=NW)
Checkbutton(f, text="Auto", variable = Auto).pack(side=LEFT,anchor=NW)
Ctrl()  ## Start control loop

Since we turned off the SysCAD level control, you can try doing this manually by adjusting the slider. Alternatively, click the Auto Checkbutton to enable a simple control strategy in the python code. (Bang-Bang Control (see Wikipedia) is not a particularly good control strategy.)



Using python and Tk, you can create elaborate control panels with plotting and many other graphical features. You can even create a button for the machine that goes "ping". (Mandatory Monty Python reference from Meaning of Life)