问题描述
正如标题所说,我想在 Windows 窗体应用程序中为 python 启动一个交互式 shell 实例。
.NET 运行时:.NET Framework 4.7.2 Python 版本:3.8.10 PythonNet 版本:3.0
这是我的 C# 代码:
using System;
using System.Windows.Forms;
using Python.Runtime;
namespace PythonInteractiveGUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
Runtime.PythonDLL = @"C:path\to\python38.dll";
PythonEngine.Initialize();
string command = System.IO.File.ReadAllText("script2.py");
using (Py.GIL())
{
using (PyScope scope = Py.CreateScope())
{
scope.Exec(command);
}
}
PythonEngine.Shutdown();
}
}
}
我的 Python 代码位于 script2.py 中:
import code
variables = globals().copy()
variables.update(locals())
shell = code.InteractiveConsole(variables)
shell.interact()
以上导致错误:
HResult=0x80131500
Message=input(): lost sys.stdin
Source=Python.Runtime
StackTrace:
<Cannot evaluate the exception stack trace>
我认为这与缺少可用的控制台窗口有关,但我不确定如何继续。这适用于在控制台应用程序下使用相同的 .NET Framework、python 和 Pythonnet 版本。
谢谢!
更新:07/06/2021 我能够使用以下接受的答案添加控制台窗口:How do I show a console output/window in a forms application?
这导致了一个单独的问题,似乎证实了我的怀疑:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)