Python Forum
integrating ironpython application with normal C# wpf
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
integrating ironpython application with normal C# wpf
#1
Hello All,
I am new to python and may be I am not able to follow python basic rules. Let me explain you guys my problem and I hope i get solution of it.
I have create VS2015 ironpython solution and it works fine. I have used ironpython wpf in this.
Now i want use this ironCalc.py file with some other normal wpf C# application. I wrote code like this:

private static void externalPyt()
{
var engine = Python.CreateEngine();
var paths = engine.GetSearchPaths();
paths.Add(@"c:\Program Files (x86)\IronPython 2.7\Platforms\Net40");
engine.SetSearchPaths(paths);
var ipy = Python.CreateRuntime();
dynamic py = ipy.UseFile(@"C:\Users\praj\Documents\Visual Studio 2015\Projects\IronCalc\IronCalc\IronCalc.py");
py.start_element();
}
private void button_Click(object sender, RoutedEventArgs e)
{
externalPyt();
}


while executing this code i get exception at "dynamic py = ipy.UseFile(@"C:\Users\praj\Documents\Visual Studio 2015\Projects\IronCalc\IronCalc\IronCalc.py");"

exception says no module named wpf found. however i have already used "using IronPython.Modules". I have also added reference of ironpython.wpf , ironpython.module in c# project but still this exception doesn't go away. Wall Wall Wall

Can anyone help and point out what is wrong in this code.

thanks
Reply
#2
Is the exception from c#, or python?  Your c# doesn't look like it should be giving that error, but I don't know a whole lot about the python interface (but I do use c# daily at work, so it's not like I just glanced over your code).

Are you referencing a wpf within your python script, that you didn't import?
Reply
#3
Hello nilamo,
As I said I am new to python as well as to this forum so let me explain again my problem with all codes using forum rules.
I have 2 solutions.
1. created by ironpython wpf with name IronCalc using interpreter Ironpython 2.7. when I run this solution it runs fine and behave as i wanted it to. Code is as follows:
import wpf
import sys
import clr
import ast
clr.AddReference("System.Windows")
from System.Windows import *
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")

from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'IronCalc.xaml')
        self.labelText.Content=''
        self.labelOut.Content=0
        
    def btnBS_Click(self, sender, e):
        labelTextLen = len(self.labelText.Content)
        self.labelText.Content = self.labelText.Content[0:labelTextLen-1]
    def btn7_Click(self, sender, e):
        self.labelText.Content=self.labelText.Content+'7'
if __name__ == '__main__':
    Application().Run(MyWindow())


2. Now I have created a C# wpf application and just want to launch previous iron python application with button created in c# wpf. While executing code I get exception that no module with name wpf Found. However in ironpython application wpf is already imported.

using System.Windows;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Modules;
using IronPython.Runtime;
using IronPython.Compiler;
using IronPython.Zlib;
namespace Test_PythonEmbeded
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private static void execPython()
        {
            var engine = Python.CreateEngine();
            var paths = engine.GetSearchPaths();
            paths.Add(@"c:\Program Files (x86)\IronPython 2.7\Platforms\Net40");
            engine.SetSearchPaths(paths);
            var ipy = Python.CreateRuntime();
            dynamic py = ipy.UseFile(@"C:\Users\dummy\Documents\Visual Studio 2015\Projects\IronCalc\IronCalc\IronCalc.py");
            py.start_element();
            
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            
            execPython();
        }
    }
}
I get execption at line 32 of above code.
Error:
An unhandled exception of type 'IronPython.Runtime.Exceptions.ImportException' occurred in Microsoft.Dynamic.dll Additional information: No module named wpf
I hope now I am able to explain my problem more clearly and hopefully will get some suggestions soon.

thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Integrating Google Drive in App Lahearle 0 464 Jul-19-2023, 05:51 PM
Last Post: Lahearle
  How to send data from a python application to an external application aditya_rajiv 1 2,173 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  IronPython from CPython amasis 3 2,206 Feb-05-2021, 12:26 PM
Last Post: jefsummers
  Scipy kolmogorov smirnov test for evaluating the fitting of a non-normal distribution mcva 0 1,986 May-26-2020, 12:01 PM
Last Post: mcva
  Integrating shango app with desktop GUI app Man_from_India 0 1,949 Aug-09-2019, 01:49 AM
Last Post: Man_from_India
  Integrating Python Script for Android App everythingisenergy 0 6,260 Jan-15-2019, 07:39 PM
Last Post: everythingisenergy
  How to fill a web form integrating with excel praveen_Akepati 0 2,600 Oct-27-2018, 06:23 AM
Last Post: praveen_Akepati
  Is this a normal/benign make test error when building python3.6 sofuego 2 3,509 Feb-12-2018, 12:32 AM
Last Post: sofuego
  Creating shadow in OpenGL with normal? hsunteik 0 3,366 Apr-27-2017, 08:23 AM
Last Post: hsunteik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020