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

Hi,

Was it supposed to be resolved in 8.7 ?
In 8.8, I still cannot move to python 3 (TypeError: Duplicate type name within an assembly.)

Also, what is the good way to force python version of a py file ?
Does “#! python X” shebang supposed to work or only extension .pyX is supported ?
The shebang does not seem to be apply unless the file is closed and re-opened again.

Regards.

@lahos

Seems to be working on my side. What is the exact error you are seeing? Also make sure to call super().__init__() in the class initializer

The language spec follows the #! python X pattern and is only used when opening a script. This is how it currently works but it could be changed if need be.

Using the .pyX is another way of specifying the language. Editor supports both.

Hi,
It seems to work. Thank you.

I forgot the super.init and yes, I experienced the need to close the file to make changing #! python 2 to 3 applied. Even if Rhino is restarted, the already opened files in script editor are not reloaded.
I run it with #! python 2, it crashed.
I restarted Rhino, changes to #! python 3, run it, it crashed.
I restarted Rhino, already with #! python 3, run it, it crashed.
I restarted Rhino, close and reopened the file, run it then it worked.

A reload in file menu could be useful to re-read interpreter change (or other interpreter pragmas).

Regards

1 Like