[GH2] ETO Forms dialog from Python component - no window shown

Rhino version: Rhino 9 WIP (Grasshopper 2)

OS: Windows

## What I’m trying to do

Show a simple Eto.Forms.Dialog from a GH2 Python component, triggered by a Boolean input.

## Step 1 - initial attempt

```python

import Eto.Forms as forms

import Eto.Drawing as drawing

import Rhino

if Show:

dialog = forms.Dialog()

dialog.Title = "GH2 Test Venster"

dialog.ClientSize = drawing.Size(300, 120)

dialog.Content = forms.Label(Text="Hallo vanuit Grasshopper 2!")

dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)

```

Result:

```

Error running script: The calling thread must be STA, because many UI components require this. [1:1]

```

This suggests GH2 Python components are (at least sometimes) executed on a non-STA thread.

## Step 2 - wrapped in RhinoApp.InvokeOnUiThread

```python

import Eto.Forms as forms

import Eto.Drawing as drawing

import Rhino

import System

def show_dialog():

dialog = forms.Dialog()

dialog.Title = "GH2 Test Venster"

dialog.ClientSize = drawing.Size(300, 120)

dialog.Content = forms.Label(Text="Hallo vanuit Grasshopper 2!")

dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)

if Show:

Rhino.RhinoApp.InvokeOnUiThread(System.Action(show_dialog))

```

Result: No error, but also no window appears (checked taskbar and Alt+Tab, nothing found).

## Step 3 - tried non-modal Show() instead of ShowModal(), plus keeping a

persistent reference via scriptcontext.sticky (to rule out early garbage

collection of the dialog object):

```python

import Eto.Forms as forms

import Eto.Drawing as drawing

import Rhino

import System

import scriptcontext as sc

def show_dialog():

dialog = forms.Dialog()

dialog.Title = "GH2 Test Venster"

dialog.ClientSize = drawing.Size(300, 120)

dialog.Content = forms.Label(Text="Hallo vanuit Grasshopper 2!")

sc.sticky\["gh2_eto_test_dialog"\] = dialog

dialog.Show()

if Show:

Rhino.RhinoApp.InvokeOnUiThread(System.Action(show_dialog))

```

Result: still no window, no error.

## Confirmed via debug output

Added an Info output printing the received Show value and its type - confirms

Show arrives correctly as Python bool True, and the code path does reach the

InvokeOnUiThread call (no exceptions thrown at any point in steps 2/3).

## Question

- Is there a currently-recommended way to show an Eto.Forms window from a GH2

Python component, given that the component’s script apparently runs off the

main UI thread?

- Is InvokeOnUiThread the correct mechanism for GH2, or is there a GH2-specific

equivalent I should be using instead?

Happy to provide more details, a minimal .ghx repro file, or test any suggested fix.

Hi vanderwee dario,

The specifics are a bit beyond me but i do have this working example if that helps.

python
import Eto.Forms as forms
import Eto.Drawing as drawing
import Rhino, System

def show_window():
    try:
        f = forms.Form()
        f.Title = "GH2 Eto"
        f.ClientSize = drawing.Size(340, 130)
        lb = forms.Label()
        lb.Text = "Shown from a GH2 Python component"
        f.Content = lb
        import builtins
        builtins._my_gh2_form = f          # keep a reference alive
        f.Owner = Rhino.UI.RhinoEtoApp.MainWindow
        f.Show()
    except Exception as e:
        print("dialog failed: %s" % e)

if Show:
    Rhino.RhinoApp.InvokeOnUiThread(System.Action(show_window))

Hi ,
Works perfect ,
Thx

Verzonden vanuit Outlook voor iOS