WPF form kills Dynamo and Civil 3D interface

I’m playing around with making some different GUI nodes with python. I have a standard python GUI that works, but I tried out using a WPF form which also works. What I’ve been having problems with is once the WPF form closes, the Dynamo window also closes and all of the UI elements in Civil 3D disappear. I end up having to close Civil 3D, which results in a crash, or ending the task to avoid the crash warnings.

Has anyone else encountered this?

I’ve attached the sample data, if anyone wants to try it out.
PythonGUItest.zip.txt (9.8 KB)

Hello @AdammReilly
here an example of use, the Wpf and Winform methods are not quite the same

import sys
import clr
import wpf
clr.AddReference("System.Xml")
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
import System
from System.IO import StringReader
from System.Xml import XmlReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application


class HelloWorld(Window):
	def __init__(self, xaml):
		self._xaml = xaml	
		xr = XmlReader.Create(StringReader(self._xaml))
		self.winLoad = wpf.LoadComponent(self, xr)
		self._comboBox = self.winLoad.FindName('pulldown')
		for x in range(1,10):
			self._comboBox.Items.Add("Item "+str(x))
		
	def okButtonPressed(self, sender, e):
		System.Windows.MessageBox.Show("HelloWorld")
		
	def CnlButtonPressed(self, sender, e):
		System.Windows.MessageBox.Show("are you leaving us already?")	
		self.Close()	

	def pulldown_SelectionChanged(self, sender, e):
		System.Windows.MessageBox.Show("you have selected {}".format(sender.SelectedItem.ToString()))	

xaml = """
<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="PythonGUItest" Height="300" Width="300"> 
       <Grid>
        <Button x:Name="btnOkay" Content="OK" HorizontalAlignment="Left" Margin="127,239,0,0" VerticalAlignment="Top" Width="75" Click="okButtonPressed"/>
        <Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Left" Margin="207,239,0,0" VerticalAlignment="Top" Width="75" Click="CnlButtonPressed"/>
        <ComboBox x:Name="pulldown" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="pulldown_SelectionChanged"/>
    </Grid>
</Window> """

hello = HelloWorld(xaml)
hello.ShowDialog()

Note:
in terms of debugging it is much easier to use Winform rather than Wpf with IronPython

7 Likes

Thanks @c.poupin That is pretty helpful.
In regards to your note at the bottom, I was only using WPF because I can use VS to design the form, I’m still too new to python to work out the code to build the UI there. But, I see that it’s a little simpler to stick with that and may need to find the time for it.

You could always design the (form) xml in VS and copy to Python as there shouldn’t be much translation.

1 Like

Thanks to @c.poupin, I was able to get this code to work:

import sys
import clr
import wpf
clr.AddReference("System.Xml")
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
import System
from System.IO import StringReader
from System.Xml import XmlReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application

sheetSets = IN[0]

class SelectSheetSet(Window):
	def __init__(self, xaml):
		self._xaml = xaml	
		xr = XmlReader.Create(StringReader(self._xaml))
		self.winLoad = wpf.LoadComponent(self, xr)
		self._comboBox = self.winLoad.FindName('pulldown')
		for x in sheetSets:
			self._comboBox.Items.Add(x.Name)
		self.output = ""
		
	def okButtonPressed(self, sender, e):
		#System.Windows.MessageBox.Show("You selected " + self._comboBox.SelectedItem)
		self.output = sheetSets[self._comboBox.SelectedIndex]
		self.Close()
		
	def CnlButtonPressed(self, sender, e):	
		self.Close()

xaml = """
<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="PythonGUItest" Height="100" Width="300">
    <Grid>
        <Button x:Name="btnOkay" Content="OK" HorizontalAlignment="Left" Margin="127,39,0,0" VerticalAlignment="Top" Width="75" Click="okButtonPressed"/>
        <Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Left" Margin="207,39,0,0" VerticalAlignment="Top" Width="75" Click="CnlButtonPressed"/>
        <ComboBox x:Name="pulldown" HorizontalAlignment="Left" Margin="162,10,0,30" Width="120"/>
        <Label Content="Select an open Sheet Set:" HorizontalAlignment="Left" Margin="10,8,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window> """

selectSet = SelectSheetSet(xaml)
selectSet.ShowDialog()
result = selectSet.output

OUT = result

This will let the user select an open sheet set, which is step one of my ultimate goal with this graph :smiley:

3 Likes