Using the PTVS for a Windows Forms Project

Chapter 8 of Professional IronPython demonstrates how to create a Windows Forms application using IronPython 2.6. As mentioned in my previous post about IronPython, IronPython 2.7 provides something new in the way of integration, PTVS. You’ve already discovered the means to create a console application using PTVS in my second post on this topic. This post goes a step further and demonstrates how to create a Windows Forms application using the same approach you’d normally use with any other .NET language, such as Visual Basic .NET or C#. I assume you already have PTVS installed on your system.

Of course, you begin by opening Visual Studio 2010 as normal. The New Project dialog box should contain a WinForms Application template in the IronPython folder. If you don’t see the correct template, then you probably don’t have IronPython 2.7 installed with Visual Studio 2010 (see my previous post for details). Call this project PythonApplication2 as shown here:

WinForm01

Click OK and Visual Studio creates the solution for you. The next step is to recreate the display shown in Listing 8-1 of the book. I had truly hoped that PTVS would provide Designer support, but it doesn’t. Here’s what you get as output from the template:

WinForm02

Now, it is disappointing that you don’t get a Designer, but the template does provide you with some helpful information and it’s better than what you started with in Chapter 8. The updated code is easier to read and requires that you perform less typing than shown in Figure 8-1. Here’s the new version of Listing 8-1.

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
 
from System.Windows.Forms import *
from System.Drawing import *
 
class MyForm(Form):
    def __init__(self):
         
        # Add a tooltip control.
        Tips = ToolTip()
 
        # Configure btnOK
        btnOK = Button()
        btnOK.Text = "&OK"
        btnOK.Location = Point(263, 13)
        Tips.SetToolTip(btnOK, 'Displays an interesting message.')
 
        # Configure btnCancel
        btnCancel = Button()
        btnCancel.Text = "&Cancel"
        btnCancel.Location = Point(263, 43)
        Tips.SetToolTip(btnCancel, 'Cancels the application.')
 
        # Configure lblMessage
        lblMessage = Label()
        lblMessage.Text = 'This is a sample label.'
        lblMessage.Location = Point(13, 13)
        lblMessage.Size = Size(120, 13)
 
        # Configure the form.
        self.ClientSize = Size(350, 200)
        self.Text = 'Simple Python Windows Forms Example'
        self.AcceptButton = btnOK
        self.CancelButton = btnCancel
 
        # Add the controls to the form.
        self.Controls.Add(btnOK)
        self.Controls.Add(btnCancel)
        self.Controls.Add(lblMessage)
 
form = MyForm()
Application.Run(form)

This code works precisely the same as the code described in Listing 8-1. You still see the same dialog box as output as shown here:

WinForm03

It isn’t anything fancy, but the example does demonstrate that you can create a Windows Forms application using straightforward code and less typing than the example in Listing 8-1. Of course, this form requires event handlers, just like the ones used for the example in Chapter 8. Of course, you begin by creating the required linkage after you initialize the form as shown here:

# Add the event handler linkage.
btnOK.Click += btnOK_Click
btnCancel.Click += btnCancel_Click

After which, you add the event handler code shown here:

# Define the event handlers.
def btnOK_Click(*args):
     
    # Display a message showing we arrived.
    MessageBox.Show('Hello!')
 
def btnCancel_Click(*args):
     
    # Close the application.
    form.Close()

At this point, you have the same first example shown in Chapter 8. Of course, Chapter 8 contains a number of other examples that demonstrate other techniques; all of which you can easily move to the new environment provided by PTVS. Here’s the source code for the entire example: Program.py. So, what do you think of the new Windows Forms 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.