[IronPython] Newbie: convert string to python expression??
fuzzyman at voidspace.org.uk
Tue Dec 16 17:19:04 CET 2008
> I'm using Ironpython with Silverlight so I can't get access to Windows.Forms
Ah - in which case it probably lives in System.Windows.Input
> Please bear with me :-) New stuff's showing up all the time. The solution
> you proposed worked but there must be something I don't get with the scope.
> Please look at my snippet below:
The advantage of a specific context is that you control what objects the
code has access to. Try setting the Matrix object into the dictionary
with the key 'm' and your code should have access to it.
> import clr, sys
> clr.AddReference("Mapack, Version=18.104.22.168, Culture=neutral,
> from Mapack import *
> def inputBox_KeyDown(s, e):
> key = e.Key.value__
> result = root.inputBox.Text
> if key == 3: #If 'Enter' key is pressed
> root.message.Text = eval(result)
> except SyntaxError:
> exec result in context
> except Exception, e:
> print 'Unhandled exception', e
> root.inputBox.Text = ""
> m = Matrix(2,2) #NOTICE: If I hard code this
> definition in the function it works
> print m #but if I try to type
> m=Matrix(2,2) in inputBox it says:
> #"Matrix is not defined"
> I can create an object of the Matrix()-class if I code it straight into the
> function as shown in my snippet above but I can't type m=Matrix(2,2) in the
> 'inputBox' and then execute it. Then I get an exception: "Matrix is not
> How can I work around this?
> Thanks very much!
> Michael Foord-5 wrote:
>> Oh - and Windows Forms has a Keys enumeration so that you don't have to
>> rely on the underlying value of the event key property.
>> from System.Windows.Forms import Keys
>> if e.Key == Keys.Enter:
>> (or something like that - check out the MSDN documentation for the
>> Michael Foord
More information about the Ironpython-users