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:
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:
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:
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.