Python 3 / windows forms

Hi,

Over the year I built a lot of tools in python using System.Windows,Forms.
It’s still usable using python 2.7 in Rhino 8.
I would like to adapt my code to cpython 3.
I bump to an error which I can’t really find info anywhere.

TypeError: Duplicate type name within an assembly.

Pointing out to the line :
class TestForm(Form):

I tried pythonnet with no success.

Any hints to solve this ?

Regards

Here is the sample code :


# python3

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Form, Application, Button, SaveFileDialog 
from System.Drawing import Point
from System.Threading import Thread, ThreadStart, ApartmentState

class TestForm(Form): # <---- TYPE ERROR
    def __init__(self):
        self.button = Button()
        self.addButton()

    def addButton(self):
        self.button.Location = Point(50,50)
        self.button.Text = "Save s.th"
        self.Controls.Add(self.button)
        self.button.Click += self.buttonClick

    def buttonClick(self, sender, event):
        dialog = SaveFileDialog()
        dialog.FileName = "test one"
        dialog.Title = "Test One"
        print("so far so good")
        dialog.ShowDialog()
        print("Never reaches this point")


def app_thread():
    app = TestForm()
    Application.Run(app)


def main():
    print('start thread')
    thread = Thread(ThreadStart(app_thread))
    print('set thread apartment STA')
    thread.SetApartmentState(ApartmentState.STA)
    thread.Start()
    thread.Join()


if __name__ == '__main__':
    main()
  
  

Thank you for reporting this! I logged it here and will get it fixed for Rhino 8.7

RH-81356 Pythonnet derived classes attempt to overload assembly-internal members

2 Likes