[IronPython] Newbie questions & comments

Martin Maly Martin.Maly at microsoft.com
Tue Dec 6 17:12:45 CET 2005


Hello Edward

> Edward K. Ream Wrote:
> 
> Alas, winforms itself does not work properly when started 
> inside my batch file.  The window freezes. Here is the 
> contents of my batch file:
> 
> cd c:\prog\IronPython-0.9.5\Tutorial
> ..\bin\IronPythonConsole c:\prog\leoCVS\leo\test\ironPythonTest.py
> 
> Here is the contents of the test file that freezes:
> 
> [starts]
> print 'Let the wild rumpus start!'
> 
> import winforms
> from System.Windows.Forms import *
> from System.Drawing import *
> 
> def click(*args): print args
> 
> f = System.Windows.Forms.Form()
> f.Click += click
> f.Show()
> 
> print 'done'
> [ends]
> 
> The window appears and 'done' is printed immediately.  
> However, clicking in the window brings up the XP "This 
> program is not responding" window. It seems that the 
> following code in winforms.py is not working as expected:
> 
> t = Thread(thread_proc)
> t.Start()


The purpose of the winforms.py in the tutorial is to start a new thread that will pump windows messages in the interactive mode so that you can construct WinForms application interactively from the console. The console thread is tied down by the character operations so it cannot pump messages. When you run an WinForms script, there is no need to set up this second thread. So the code above would work well if entered interactively from the console.


> Here is another attempt:
> 
> [starts]
> print 'Let the wild rumpus start!'
> 
> import sys
> import System
> sys.LoadAssemblyByName("System.Windows.Forms")
> sys.LoadAssemblyByName("System.Drawing")
> 
> def click(*args): print args
> 
> f = System.Windows.Forms.Form()
> f.Click += click
> f.Show()
> 
> print 'done'
> [ends]
> In this case the window appears (very fleetingly, I think), 
> and the 'done' 
> message appears and that's all.  Presumably this is 
> 'correct': the code wasn't run in a separate thread.


This is actually almost exactly what you need. There is one line missing at the end, after you show the form:

System.Windows.Forms.Application.Run()

That will start the message pump and your window will stay up, receiving messages.

Alternatively, you can follow the C# sample below and run the following script:

import sys
import System
sys.LoadAssemblyByName("System.Windows.Forms")

def click(*args): print args
 
f = System.Windows.Forms.Form()
f.Click += click
System.Windows.Forms.Application.Run(f)
print 'done'

The advantage being that if you close the form, the message loop will terminate. In the first case, you have to terminate the application explicitly by calling Application.Exit() from one of your event handlers, for example:


import sys
import System
sys.LoadAssemblyByName("System.Windows.Forms")

def click(*args): System.Windows.Forms.Application.Exit()
 
f = System.Windows.Forms.Form()
f.Click += click
f.Show()
System.Windows.Forms.Application.Run()


> Consider the Windows Forms tutorial at:
> 
> http://samples.gotdotnet.com/quickstart/winforms/
> 
> The c# code is:
> 
> namespace Microsoft.Samples.WinForms.Cs.SimpleHelloWorld {
>     using System;
>     using System.Windows.Forms;
> 
>     public class SimpleHelloWorld : Form {
> 
>         [STAThread]
>         public static int Main(string[] args) {
>             Application.Run(new SimpleHelloWorld());
>             return 0;
>         }
> 
>         public SimpleHelloWorld() {
>             this.Text = "Hello World";
>         }
>     }
> }
> 
> Obviously, something involving threads is happening, but a 
> newbie isn't going to know how to do this in IronPython.  
> BTW, I assume eventually there will be a Python tab for all 
> these examples :-)
> 
> So from my mostly ignorant viewpoint it appears that:
> 
> - there is a problem with the thread code in winforms.py
> 
> - the IronPython tutorial doesn't tell how to run a Hello 
> World IronPython program from a batch file.
> 


Thanks for this feedback, we'll think about how to make the examples clearer.

Martin


More information about the Ironpython-users mailing list