Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Python

...

.print()

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

...

But running when executing python code inside GiD, this console doesn’t does not exists, and then thus, this command cannot be used, except in case that the IDLE shell is opened and then the output of print is showed therewhen using IDLE shell ( Utilities -> Tools -> Develop -> 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:

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

Python show text with GiD’s proc W

If Python is running in GiD then can call the Tcl GiD procedure called W that show W can be called, which shows text in a window.
e.g.

Code Block
languagepy
GiD_Python_Exec {
  import tohil
  tcl=tohil.import_tcl()    ;# defines in python all defined Tcl proc's at this time
  a=5.2
  tcl.W(a)
  b=a*3
  tcl.W(b)
}

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

...

Note: Important note:

Tcl follows a ’lazy' approach, this means that at a certain point in time of execution, only the procedures do exist that have been called or that have been defined from the current or previous loaded scripts.

tcl = tohil.import_tcl() is a tohil module command that uses Tcl’s introspection capabilities to create creates a Python object functions with methods for each tcl Tcl proc and commandcommands, so that calling the Tcl procs looks very much like calling any Python function.
Warning: the Python functions created depends on the procs existing when tcl = tohil.import_tcl() is invoked. If a proc like W is already not defined in Tcl during this call, the function tcl.W() won’t exists!! uses Tcl’s introspection capabilities to map Tcl procs and commands to methods of the Python object. It will only map existing Tcl procs and commands at the time of invocation!
This means that the above code will fail if the Tcl proc W has never been called before.
To make sure the above code works, first execute -np- W "Hello World" in GiD’s command line and then execute the above code.

Using tohil.call('W','hello world') will work as it delegates to the internal tohil Tcl interpreter the execution of the string list parameters, instead of calling the Tcl-proc directly. This way the internal Tcl interpreter will look for the definition of the procedure W if it does not exists, and execute it with 'hello world' as parameter.

tcl.W() can also be called from the IDLE shell window of GiD Utilities-->Tools-->Python console...:

...

Warning: It seems that tcl.W() tcl’s command update 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, showing following message ( MS Windows and Linux):

...

But if you click ‘OK’ the code will continue its execution.
Be aware that the command update is used in several GiD's windows and utilities, so this error message will appear several times.
Note: on macOS tcl’s command update will cause GiD likely to crash.

And in windows also crash, to avoid it we have modified the source code of Modules\_tkinter.c and recompiled.