C# .NET windows form example with python calls

See original GitHub issue

I would like to ask that would it be possible to have an example .NET winform application which imports some python module, (ie numpy) and makes some call from it? I could not realy find such anywhere, and my trial fails right at the begining with

An unhandled exception of type ‘System.ArgumentNullException’ occurred in mscorlib.dll

Additional information: Value cannot be null.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Python.Runtime;

public partial class frmMain : Form
{
    public frmMain()
    {
        InitializeComponent();

        PythonEngine.Initialize(); //<- Error occurs here.
    }
}

Error details: error

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
denfromufacommented, Feb 1, 2017
1reaction
tarpistacommented, Jan 12, 2017

Yes, I checked troubleshooting wiki, but could not succeed with it. Finally I found a workaround by using pythonnet in the other direction (as almost everyone does.), ie making .NET calls from python.

Basically now I make my gui in .NET (C#), and import it in python. In example my C# code looks like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace NeuralNetsGUI
{
    public partial class frmMain : Form
    {
        public Action fp_btn_LoadData_Click;

        public frmMain()
        {
            InitializeComponent();
        }

        private void btn_LoadData_Click(object sender, EventArgs e)
        {
            if (fp_btn_LoadData_Click != null)
            {
                new Thread(new ThreadStart(fp_btn_LoadData_Click)).Start(); //Asynchronus call
                //fp_btn_LoadData_Click.DynamicInvoke(); //Synchronous call
            }
        }
    }
}

As a post-build event in Visual Studio, I copy the compiled NeuralNetsGUI.exe file into my python directory, so it is possible to import it in python:

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("NeuralNetsGUI") 

import System.Windows.Forms as WinForms
import NeuralNetsGUI as NetsGUI
from System import Action


class clsNetsGUI():
    """A class that creates windows form by importing NetsGUI.frmMain()"""

    def __init__(self):
        self.MainForm = NetsGUI.frmMain()
        self.MainForm.fp_btn_LoadData_Click = (Action(self.btn_LoadData_Click)) # <-- this is where I set the event handler function 

        app = WinForms.Application
        app.Run(self.MainForm)

    def btn_LoadData_Click(self):
        """Button click event handler"""
        print("Button clicked!")

def main():
    netsgui = clsNetsGUI()


if __name__ == '__main__':
    main()
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Executing Python Script from Windows Forms .NET
The basic idea of my project is a desktop applicaton and the overall logic would be to read from a couple of selected...
Read more >
How to Build Windows Forms Applications in Python | Pt. 1
In thise series, we will build a Windows Form application using ... NET API Browser Link: https://docs.microsoft.com/en-us/dotnet/api/?view= ...
Read more >
Calling Python from C#: an introduction to PythonNET
Calling Python from C#: an introduction to PythonNET ... In this way we can import any class, including C#'s System classes, and use...
Read more >
How to call a Python function from C#
To call a Python function from C#, we'll use the Python.NET library. This package allows us to integrate Python with .NET projects and...
Read more >
C# Windows Forms Application Tutorial with Example
Below is an example of a simple Windows form application C#. It shows a simple Login screen, which is accessible by the user....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found