GiD - The personal pre and post processor

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 14 Next »

Python.print()

When Python is executed externally, in Windows, a DOS-console is opened and it is possible to use the Python function print() to show values in this console:

But when executing python code inside GiD, this console does not exists, and thus, this command cannot be used, except when using IDLE shell ( Utilities --> Tools → Console Python… ) .

Python print to file

To debug Python code it is always possible (inside and outside GiD) to print data of variables to a file, with something like this:

f=open('C:/temp/my_debug.txt','a')
f.write('hello world\n')
f.close()

Python to show text with the GiD proc W

If Python is running in GiD then the Tcl GiD procedure W can be called, which shows text in a window

e.g.

GiD_Python_Exec {
  import tohil
  tcl=tohil.import_tcl()
  a=5.2
  tcl.W(a)
  b=a*3
  tcl.W(b)
}

will show a GiD window with the value of the variables 'a' and 'b'

Note: tcl = tohil.import_tcl() is a tohil module command that uses Tcl’s introspection capabilities to create Python object methods for each Tcl proc and command, so that calling the Tcl procs looks very much like calling any Python function.

Warning: the Python methods created depends on the procs existing when tohil.import_tcl() is invoked. If a proc like W is already not defined in Tcl during this call, the Python method tcl.W('hello world') won’t exists, but tohil.call('W','hello world') will works.

Warning: It seems that tcl.W() cannot be called from the IDLE shell window or will crash

By now do not try something like this or GiD will crash:

In this case it is less interesting to be used, because can use print(). The most interesting case is to use to debug code of GiD_Python_Exec, or GiD_Python_Source

  • No labels