Bring Python to Your .NET Development with IronPython

Python is a high-level programming language that has gained popularity in recent years for its emphasis on clear code that is easy to read, combined with surprising power and flexibility. Because Python is free and open-source, it has become a widely used scripting language primarily for web-based applications…but did you know that a little help from the .NET framework can bring your Python apps to the desktop, complete with a graphical user interface? IronPython is a handy tool that will allow you to enjoy the perks of .NET development with your favorite language, Python.

IronPython is a version of Python that is tightly integrated with .NET, originally developed and maintained by a team of Microsoft engineers but recently released to the open-source community. IronPython integrates with Microsoft Visual Studio and allows you to combine traditional Python code with .NET technologies, including Windows Forms and WPF for UI design. The result is a Python-coded application that looks and behaves no differently than any other Windows program, which is a big improvement over the command-line programs typical of the language.

'Hello World' with IronPython

But how exactly does IronPython fit into the .NET world? The diagram below shows the basic functionality where the Python code makes calls to both the .NET framework classes as well as Python libraries. The Python code is then compiled by the IronPython Engine and converted to assembly code that can be executed by the .NET runtime.

IronPython isn’t just for the desktop and can be used to develop web applications that integrate with Silverlight, a Microsoft framework similar to Adobe Flash. If you’re worried about pigeon-holing yourself into Windows with .NET, fear not, IronPython is supported by Mono, an open-source and cross-platform alternative to .NET. Likewise, if you don’t have the money to throw down on a license for Visual Studio, IronPython Studio is a free IDE that runs from the Visual Studio Shell.

So, if you’re a Python developer and want to make user-friendly apps that can take advantage of all that .NET has to offer, bust out of your command-line world give IronPython a spin.

How to Stretch a Menu Control to the Width of a Window in WPF

If you’ve ever created a Menu in WPF, you’ve probably noticed something peculiar: A Menu will not stretch to fill the entire width of its parent container. If you set the Menu’s width to ‘auto’ or delete the width property altogether, the Menu will shrink to the maximum width needed by its MenuItem elements (so if you have 0 MenuItems, it will disappear completely).


	
	
	
	

A Menu in WPF only stretches to the width of its children by default.

This is contrary to the default behavior of a Toolbar control, which will automatically stretch to the width of its parent container.


	

The easiest solution I’ve found to deal with this problem is to manually bind the width of the Menu to its parent container’s width. To do this, you’ll first need to give the Menu’s parent container a name (I’m using a Grid as the parent, so I called it “grid_Main”) and then add the following code to your Menu:

Width="{Binding ElementName=grid_Main,Path=ActualWidth}"

Which in my application looks like this:


	
		
	

This will stretch your menu to the full length of its parent container, which in my case is the entire window.

If you prefer to do this programatically in C#, you can set the Menu’s width to equal the parent container’s width after the parent container has loaded (doing so before the parent container has loaded would set the Menu’s width to 0).

To do this, I created a Loaded event in my Menu in WPF:


And in the event, I set the Menu’s width to the ActualWidth property of its parent container, which is the same Grid I used in the earlier example.

private void menu_Main_Loaded(object sender, RoutedEventArgs e)
{
	this.menu_Main.Width = this.grid_Main.ActualWidth;
}

This has the same effect as binding the element properties in WPF, but we’ve overlooked one important thing: since the width of the Menu is only set when the control is loaded, it will not change if you resize the Window after the application is running.

To deal with this, I found it easiest to create a SizeChanged event on the main Window:




And in the SizeChanged event, once again set the width of the Menu to the width of its parent container as we did before.

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
	this.menu_Main.Width = this.grid_Main.ActualWidth;
}

This will give you the same functionality as the WPF solution, although it requires some additional code.