0

I want to use a GUI made with C# WinForms in Python.

Here are the versions I am using:

Python: 3.12 Pythonnet: 3.0.5 For C#, I used Visual Studio 2022. When creating the project, I selected "WinForms" and wrote the code as follows:

enter image description here

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("hello");

        }

        public static void ShowForm()
        {
            Application.Run(new Form1());
        }

    }
}

After building the solution in C#, I copied the DLL file from the Debug folder into the Python project folder.

For Python, I am using Visual Studio Code.

enter image description here

import clr
import os



def test():

    clr.AddReference(r"./WinFormsApp1.dll")


    from WinFormsApp1 import Form1


    Form1.ShowForm() 


if __name__ == "__main__":
    test()

from WinFormsApp1 import Form1

As shown in the image, this part is not recognized.

What should I do?

4
  • 1
    I really have no idea. However I'm curious about you having this hard-coded: "./WinFormsApp1.dll". Why do you think the dll is in that folder. Have you tried to write some Python code to see what that folder "." is, and checking to make sure the dll is there. Try reading a text file from where you expect it to be. If you can get the full name of a file from a relative name in Python, see what you see Commented Jan 18 at 5:02
  • Why not use a python gui framework and save yourself from headaches? Commented Jan 18 at 7:38
  • @Flydog57 The file is definitely in the same folder. Even when using the absolute path, the same issue persists. Commented Jan 18 at 15:15
  • stackoverflow.com/questions/2077870/… Commented Jan 21 at 12:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.