[Python.NET] bridging C# and Python
Feihong Hsu
hsu.feihong at yahoo.com
Wed Nov 28 17:25:33 CET 2007
Have you tried using callbacks through delegates instead? In practice I haven't had any problems doing stuff like that. For example the following works fine:
def callback(sender, args):
print 'You clicked on', sender.Text
btn = Button()
btn.Text = 'Click me!'
btn.Click += callback
Doing it this way means the C# code doesn't need to worry about how to find any Python modules.
- Feihong
Matt Mackowski <matt.mackowski at slipg8.com> wrote: Hi,
I would like to run a python application have it call a C# application and have the two applications be able to call functions in the other application.
I have gotten the python to open and run the C# application and I am able to run any function from the C# app in python.
What I am having trouble with is calling python functions from the C# when the C# app was started by a python call. When I run the C# alone I can call the python functions.
Part of my problem I think is that the C# app doesnt know where to fine the python modules. Where do they need to be if I want to use the PythonEngine.ImportModule?
In python my codes looks like this
import clr
import pythoncom
clr.AddReference("ScriptEditor")
from ScriptEditor import ScriptEditor
self.app = ScriptEditor()
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Application
Application.Run(self.app)
ScriptEditor is my C# app.
I can call most of the C# functions from python with the above code. The C# functions I cannot call are ones that use python for example
//-------------------------------------------------------------------------------------------------
public void usingPython()
//-------------------------------------------------------------------------------------------------
{
PythonEngine.Initialize();
IntPtr pyLock = PythonEngine.AcquireLock();
PyObject pyObj = PythonEngine.ImportModule("hello");
pyObj.InvokeMethod("helloCallback", new PyTuple());
PythonEngine.ReleaseLock(pyLock);
PythonEngine.Shutdown();
}
That doesnt work. It works when I call the function in C#.
Hello.py looks like this:
def hello():
return 'hello'
def helloCallback():
print "This is a Test and Of The python.net"
Thanks
Matt
_________________________________________________
Python.NET mailing list - PythonDotNet at python.org
http://mail.python.org/mailman/listinfo/pythondotnet
---------------------------------
Get easy, one-click access to your favorites. Make Yahoo! your homepage.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythondotnet/attachments/20071128/5c93839a/attachment.htm
More information about the PythonDotNet
mailing list