[Python.NET] Creating Menu Items

Brian Lloyd brian at zope.com
Tue Jul 5 16:51:01 CEST 2005


> According to the CLR spec, MenuItem objects have constructors in the form:
> 
> MenuItem(Text, Handler)
> MenuItem(Text, Handler, Shortcut)
> 
> Yet using either of these in PythonNET i get a blank menu item, any
> ideas why this isnt working?
> 
> sample:
> 	def InitializeComponent(self):
> 		FileOpen = 
> WinForms.MenuItem("&Open",self.OnFileOpen,WinForms.Shortcut.CtrlO)
> 		FileSave = 
> WinForms.MenuItem("&Save",self.OnFileSave,WinForms.Shortcut.CtrlS)
> 		FileExit = 
> WinForms.MenuItem("E&xit",self.OnFileExit,WinForms.Shortcut.AltF4)
> 		
> 		FileMenu = WinForms.MenuItem("&File", 
> (FileOpen,FileSave,FileExit) )	
> 		
> 		self.Menu = WinForms.MainMenu( (FileMenu,) )
> 
> This produces a menu bar with a File menu having 3 entries, but all
> three are blank.

Here is what appears to be happening: the handler argument 
wants to be an instance SystemEventHandler. Python for .NET 
does not automagically create the delegate for you in this 
case, so it fails to match the (string, EventHander, Shortcut) 
overload and falls back to the no-args constructor.

The fix is to pass System.EventArgs(self.OnFileOpen), wrapping the 
callbacks in the appropriate delegates.

It probably could be argued that pythonnet should convert a callable 
automatically when a delegate type is required -- I'd want to think 
about any possible side-effects of that and work out what level of 
error checking (sig-checking on the callable) would be appropriate 
first though...

HTH,

Brian Lloyd        brian at zope.com
V.P. Engineering   540.361.1716              
Zope Corporation   http://www.zope.com 




More information about the PythonDotNet mailing list