[IronPython] Issue with using multiple forms in IronPython
Leonides Saguisag
lsaguisag at vmware.com
Wed Oct 7 22:36:28 CEST 2009
I am trying to create a simple app which has two forms. Form1 contains a button which, when clicked, should display Form2 as a dialog. Here is the source code (developed using IronPython Studio):
### Program.py ###
from System import *
from System.Windows.Forms import *
from Form1 import *
class WindowsApplication80: # namespace
@staticmethod
def RealEntryPoint():
Application.EnableVisualStyles()
Application.Run(WindowsApplication8.Form1())
if __name__ == "Program":
WindowsApplication80.RealEntryPoint();
### end of Program.py ###
### Form1.py ###
import System
from System.Windows.Forms import *
from System.ComponentModel import *
from System.Drawing import *
from clr import *
from Form2 import *
class WindowsApplication8: # namespace
class Form1(System.Windows.Forms.Form):
"""type(_button1) == System.Windows.Forms.Button, type(_form2) == System.Windows.Forms.Form"""
__slots__ = ['_button1', '_form2']
def __init__(self):
self.InitializeComponent()
@accepts(Self(), bool)
@returns(None)
def Dispose(self, disposing):
super(type(self), self).Dispose(disposing)
@returns(None)
def InitializeComponent(self):
self._button1 = System.Windows.Forms.Button()
self.SuspendLayout()
#
# button1
#
self._button1.Location = System.Drawing.Point(96, 78)
self._button1.Name = 'button1'
self._button1.Size = System.Drawing.Size(75, 23)
self._button1.TabIndex = 0
self._button1.Text = 'button1'
self._button1.UseVisualStyleBackColor = True
self._button1.Click += self._button1_Click
#
# Form1
#
self.ClientSize = System.Drawing.Size(292, 273)
self.Controls.Add(self._button1)
self.Name = 'Form1'
self.Text = 'Form1'
self.ResumeLayout(False)
@accepts(Self(), System.Object, System.EventArgs)
@returns(None)
def _button1_Click(self, sender, e):
self._form2 = WindowsApplication8.Form2()
ShowDialog(self._form2)
### end of Form1.py ###
### Form2.py ###
import System
from System.Windows.Forms import *
from System.ComponentModel import *
from System.Drawing import *
from clr import *
class WindowsApplication8: # namespace
class Form2(System.Windows.Forms.Form):
__slots__ = []
def __init__(self):
self.InitializeComponent()
@accepts(Self(), bool)
@returns(None)
def Dispose(self, disposing):
super(type(self), self).Dispose(disposing)
@returns(None)
def InitializeComponent(self):
self.SuspendLayout()
#
# Form2
#
self.ClientSize = System.Drawing.Size(292, 273)
self.Name = 'Form2'
self.Text = 'Form2'
self.Load += self._Form2_Load
self.ResumeLayout(False)
### end of Form2.py ###
The issue is that when I click on button1 on Form1, the debugger stops in the _button1_Click method, on the following line:
self._form2 = WindowsApplication8.Form2()
Here is the exception detail:
### Start of exception detail ###
IronPython.Runtime.Exceptions.PythonNameErrorException was unhandled by user code
Message="name 'Form2' not defined"
Source="IronPython"
StackTrace:
at IronPython.Runtime.Operations.Ops.CheckInitializedOrBuiltin(Object o, ICallerContext context, String name)
at Form1._button1_Click$f630(FunctionEnvironment8Dictionary $env, Object self, Object sender, Object e) in Form1.py:line 49
at IronPython.Runtime.Calls.Function3.Call(ICallerContext context, Object arg0, Object arg1, Object arg2)
at IronPython.Runtime.Calls.Function3.Call(ICallerContext context, Object[] args)
at IronPython.Runtime.Operations.Ops.Call(Object func, Object[] args)
at IronPython.Modules.ClrModule.ReturnChecker.RuntimeChecker.Call(Object[] args)
at IronPython.Runtime.Operations.Ops.Call(Object func, Object[] args)
at IronPython.Modules.ClrModule.ArgChecker.RuntimeChecker.Call(Object[] args)
at IronPython.Runtime.Operations.Ops.Call(Object func, Object[] args)
at IronPython.Runtime.Types.ReflectedEvent.EventDispatcher.Call(Object[] args)
at IronPython.Runtime.Operations.Ops.Call(Object func, Object[] args)
at IronPython.Runtime.Operations.Ops.Call(Object func, Object arg0, Object arg1)
at System.Void(Object, EventArgs)##51(Object , Object , EventArgs )
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Run##60(Object )
at IronPython.Runtime.Calls.FastCallable1.Call(ICallerContext context, Object arg0)
at IronPython.Runtime.Calls.FastCallable1.Call(ICallerContext context, Object[] args)
at IronPython.Compiler.MethodBinder.MethodTarget.Call(ICallerContext context, Object[] args)
at IronPython.Compiler.MethodBinder.TargetSet.Call(ICallerContext context, CallType callType, Object[] args)
at IronPython.Compiler.MethodBinder.TargetSet.Call1(ICallerContext context, Object arg0)
at IronPython.Runtime.Calls.FastCallableWithContextAny.Call(ICallerContext context, Object arg0)
at IronPython.Runtime.Calls.BuiltinFunction.Call(ICallerContext context, Object arg0)
at IronPython.Runtime.Operations.Ops.CallWithContext(ICallerContext context, Object func, Object arg0)
at Program.RealEntryPoint$f634(FunctionEnvironment4Dictionary $env) in Program.py:line 10
at IronPython.Runtime.Calls.Function0.Call(ICallerContext context)
at IronPython.Runtime.Operations.Ops.CallWithContext(ICallerContext context, Object func)
at Program.Initialize() in Program.py:line 13
at IronPython.Runtime.Operations.Ops.ExecuteCompiled(InitializeModule init)
InnerException:
### End of exception detail ###
I am stumped as to why it thinks Form2 is not defined.
Just to add, all of the files are all located in the same folder:
WindowsApplication8\Form1.py
WindowsApplication8\Form1.resx
WindowsApplication8\Form2.py
WindowsApplication8\Form2.resx
WindowsApplication8\Program.py
I would greatly appreciate any insight anyone can provide.
Thanks,
-- Leo
More information about the Ironpython-users
mailing list