Using the PTVS for a WPF Project

As with Silverlight, there is no mention of theWindows Presentation Foundation (WPF) in my book, “Professional IronPython.” The WPF is supposed to help solve some display problems for application developers and allow better use of modern graphics processors. Microsoft has provided a significant amount of information about the WPF that I won’t be discussing in this post. Whether you find WPF helpful or not depends on the kind of applications you create. I’m sure that WPF will find use in game applications, widgets of various sorts, mini-applications, and a host of other uses, but I’m not sure just what it adds to the common business application, such as a data entry application. Still, WPF is an exciting new technology that you should investigate.

As with the Silverlight project in my previous post, you need to have the PTVS installed on your system to work through this example. To begin this example,
you start Visual Studio as normal and choose File | New to create a new
project. The New Project dialog box should contain an entry for a Wpf Application template as shown here. If you don’t see
this template, then you probably don’t have IronPython 2.7 installed on
your system and can use the instructions found in the IronPython 2.7 and PTVS blog entry to install it.

WPF01

The example will accept the default name for the project. Of course, you can use any name that you’d like. Click OK. You’ll see a new WPF application. However, for some odd reason, the eXtensible Application Markup Language (XAML) (pronounced zammel) file doesn’t always open, so you’ll have to open it manually. At this point, you’ll see a project similar to the one shown here.

WPF02

The top half of the designer shows what the page will look like, while the bottom half shows the associated XML. As you add controls to the form and configure them, you’ll see the changes in the XML, making it easy to code changes to the controls when necessary. Of course, you can choose to display just the Design window or just the XAML window if desired. Depending on the amount of screen real estate at your disposal, collapsing one of the panes by clicking Collapse Pane at the right side of the window, may be a good idea.

One of the advantages of using WPF is that you have immediate access to the Ribbon without having to employ any odd programming techniques. You just drag and drop the Ribbon controls from the Toolbox as you would any other control. Here’s a subset of the default set of WPF controls you can access from IronPython.

WPF03
As you can see, the set of controls is rich and you do have access to others, so WPF may be the way to go for IronPython developers who need maximum control access.

The example isn’t going to do anything fancy. I’ve added two Button controls to the form. Here is how I set the various properties using XAML:

<Window
   Title="WpfApplication1" Height="300" Width="300">
   <Grid>
      <Button Content="Click Me"
              Height="23"
              HorizontalAlignment="Left"
              Margin="191,12,0,0"
              VerticalAlignment="Top"
              Width="75"
              Uid="btnDisplay"
              Click="btnMessage_Click" />
      <Button Content="Cancel"
              Height="23"
              HorizontalAlignment="Left"
              Margin="191,41,0,0"
              VerticalAlignment="Top"
              Width="75"
              Uid="btnCancel"
              Click="btnCancel_Click" />
   </Grid>
</Window>

The first button displays a simple message box, while the second ends the application. Each Button control is assigned basic visual properties, a name, and an event handler for its Click() event. One thing I dislike about working with WPF is that the Properties window doesn’t display any quick help for the various properties. You need to know what each property does or look it up, which is really inconvenient.

The code behind for this example relatively simple. As previously mentioned, the first control displays a message, while the second closes the application as shown here:

import wpf
 
from System.Windows import Application, Window, MessageBox
 
class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'WpfApplication1.xaml')
     
    def btnCancel_Click(self, sender, e):
         
        # End the application.
        MyWindow.Close(self)
     
    def btnMessage_Click(self, sender, e):
         
        # Display the message on screen.
        MessageBox.Show("Hello")
     
if __name__ == '__main__':
    Application().Run(MyWindow())

Much of this code is automatically generated for you. In order to use the MessageBox class, you must add it to the import statement as shown in the code. The code begins with the initialization, which simply loads the components from WpfApplication1.xaml.

The btnCancel_Click() event handler relies on the MyWindow object’s Close() method. Make sure you pass a copy of self as part of the call.

The btnMessage_Click() event handler makes a call to the MessageBox.Show() method with a simple text message. You’ll see the dialog box appear as expected.

From what I can see, the WPF support for IronPython in PTVS is on par with WPF support for any other .NET language. It has the same limitations and benefits as any WPF application will have. So, what do you think of the new WPF project support provided by PTVS? Let me know at John@JohnMuellerBooks.com.

 

Author: John

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 99 books and over 600 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Web security book, discussions of how to manage big data using data science, a Windows command -line reference, and a book that shows how to build your own custom PC. His technical editing skills have helped over more than 67 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He has also contributed articles to magazines such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John’s blog at http://blog.johnmuellerbooks.com/. When John isn’t working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature. John also likes making wine and knitting. When not occupied with anything else, he makes glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at John@JohnMuellerBooks.com. John is also setting up a website at http://www.johnmuellerbooks.com/. Feel free to take a look and make suggestions on how he can improve it.