Next Previous Contents

2. Writing plug-ins in Python

The simplest way to write a plug-in is to write a Python module in Python. A Python module is simply any text file containing Python code, residing in the Python module import path, sys.path Please see the documentation at www.python.org for more information. The Python commands available to you for controlling SciGraphica functions are defined in the pysga.py file. Let's look at an example first:


import sg       
def function():
  print("Test module")  
sg.register_plugin("Test module","Worksheet:",function)

The first line ensures the sg module is included in your module's namespace. This module is the SciGraphica Python binding. The next two lines simply defines a function that will be called when our module gets selected. Save your module source as e.g. mytest.py.

Line 4 registers the plug-in to be available under the " Worksheet"

menu, displayed by right-clicking in any worksheet. When your module, named just mytest when it is imported, a new item will appear in this menu, named " Test module" . To test your module, open the SciGraphica terminal from the Scripting- >Terminal menu. Then open a worksheet, right-click on one of the cells, and select the " Test module" item from the menu. If all went well, you should see the words Test module should appear in the terminal.

Two main groups are currently defined, namely Worksheet, and Plot, with the latter denoting the right-click menu for plots.

2.1 Writing gtk+ GUIs in Python

If you have the pygtk module installed on your system, you have the full power of gtk+ at your disposal. Two simple rules apply when writing GUIs for your plug-ins using pygtk: 1) Do not initialize start your own event loop by calling gtk.mainloop() and 2) do not jump out of the event loop by calling gtk.mainquit(). This is also true for constructing dialogs: constructing modal dialogs of your own main dialog should be done by creating a new window, and letting the SciGraphica event loop process events for your dialog.


#import gtk and sg modules
from gtk import * 
import sg

def fl(button): 
print("Button pressed")

def test_window(text="Test"): 
  w=GtkWindow() 
  button=GtkButton(text)
  button.connect("clicked", fl)
  w.add(button)
  w.show_all()

sg.new_plugin_group("Worksheet:Test:")
sg.register_plugin("Testmodule","Worksheet:Test:",test_window)


Next Previous Contents