Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use IronPython 2.7.5.0, I want to use this tool to create a compiler, First I have some problem to get the outPut info from IronPython. Could someone give me some help ?

C#
using System;
using System.Windows;
using IronPython.Hosting;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var strExpression = @"
import sys
sys.stdout=my
for i in range(1, 5):
	print i ";
                var engine = Python.CreateEngine();
                var scope = engine.CreateScope();
                var sourceCode = engine.CreateScriptSourceFromString(strExpression);
                scope.SetVariable("my", this);
                var actual = sourceCode.Execute(scope);
                TextBlock.Text += actual;
            }
            catch (Exception ex)
            {
                //TextBlock.Text += ex.ToString();
                MessageBox.Show(ex.ToString());
            }
        }

        public bool softspace;
        public void write(string s)
        {
            TextBlock.Text += s;
        }
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900