1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
<TITLE>Modules and Plug-ins in SciGraphica: Writing plug-ins in Python</TITLE>
<LINK HREF="Modules-1.html" REL=previous>
<LINK HREF="Modules.html#toc2" REL=contents>
</HEAD>
<BODY>
Next
<A HREF="Modules-1.html">Previous</A>
<A HREF="Modules.html#toc2">Contents</A>
<HR>
<H2><A NAME="s2">2. Writing plug-ins in Python</A></H2>
<P>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, <CODE>sys.path</CODE> Please
see the documentation at <CODE></CODE><CODE><EM>www.python.org </EM> </CODE>for more information. The
Python commands available to you for controlling SciGraphica functions
are defined in the <CODE>pysga.py</CODE> file. Let's look at an example first:
<P>
<P>
<HR>
<PRE>
import sg
def function():
print("Test module")
sg.register_plugin("Test module","Worksheet:",function)
</PRE>
<HR>
<P>The first line ensures the <CODE>sg</CODE> 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. <CODE>mytest.py.</CODE>
<P>Line 4 registers the plug-in to be available under the "
Worksheet"
<P>menu, displayed by right-clicking in any worksheet. When your module,
named just <CODE>mytest</CODE> 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 <CODE>Test module</CODE>
should appear in the terminal.
<P>Two main groups are currently defined, namely <CODE>Worksheet</CODE>, and
<CODE>Plot</CODE>, with the latter denoting the right-click menu for plots.
<H2><A NAME="ss2.1">2.1 Writing gtk+ GUIs in Python</A>
</H2>
<P>If you have the <CODE>pygtk</CODE> 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 <CODE>gtk.mainloop()</CODE> and 2) do not
jump out of the event loop by calling <CODE>gtk.mainquit(). </CODE>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.
<P>
<HR>
<PRE>
#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)
</PRE>
<HR>
<P>
<P>
<HR>
Next
<A HREF="Modules-1.html">Previous</A>
<A HREF="Modules.html#toc2">Contents</A>
</BODY>
</HTML>
|