title: GUI development with IronPython and Visual Studio 2010
body: <p>I'm teaching an introductory class to programming and GUI development using Python, and have found that the least overwhelming solution for students new to programming is to use Visual Studio for GUI development.</p>
  
  <p>While the GUI development experience with C# and VB is pleasant, I couldn't find a way to do the same with IronPython. I installed IronPython 2.7.1 which includes the Visual Studio tools, and created a WPF IronPython project.</p>
  
  <p>I can use the WPF form designer just like VB and C#, however, I couldn't find a convenient way (i.e., comprehensible to the students) in which the GUI elements could be accessed. For example, with VB, you can refer to elements based on their name and then you can modify properties within them. The best I could do with IronPython (which I don't plan to show to the students) is the following:</p>
  
  <pre><code>import wpf
  
  from System.Windows import Application, Window
  
  class MyWindow(Window):
      def __init__(self):
          wpf.LoadComponent(self, 'WpfApplication3.xaml')
  
      def Button_Click(self, sender, e):
          #This is the only way I could find in which I can 
          #access an element and modify its properties
          self.Content.Children[1].Text += 'Hello World\n'
  
  
  if __name__ == '__main__':
      Application().Run(MyWindow())
  </code></pre>
  
  <p>I noticed that the GUI elements do not get a name and Visual Studio crashes whenever I try to manually modify the XAML to name elements. Here is the generated XAML for a simple frame with a button and text area:</p>
  
  <pre><code>&lt;Window 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Title="WpfApplication3" Height="300" Width="300"&gt; 
      &lt;Grid&gt;
          &lt;Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="103,226,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" /&gt;
          &lt;TextBox Height="182" HorizontalAlignment="Left" Margin="24,21,0,0" VerticalAlignment="Top" Width="237" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt; 
  </code></pre>
  
  <p>Any assistance in making this easier on the students would be appreciated. I'm also open to other suggestions for Python GUI development which offer an experience similar to Visual Studio.</p>