1ch*_*nar 2 .net python wpf ironpython pyc
我使用 IronPython 2.7 和 WPF 使用 VS2017 开发了一个应用程序,使用 ipy 命令可以正常工作。我想从项目创建一个 exe 文件,所以我在 cmd 中使用了以下命令:
ipy pyc.py /main:IronPython5.py /target:winexe
Run Code Online (Sandbox Code Playgroud)
项目的哪个 XAML 文件(包括所有相关 DLL)位于部署文件夹中,但是,我收到以下错误,我无法理解其含义:
Traceback (most recent call last):
File "pyc.py", line 332, in <module>
File "pyc.py", line 327, in Main
File "pyc.py", line 181, in GenerateExe
SystemError: Ambiguous match found.
Run Code Online (Sandbox Code Playgroud)
Ironpython5.py 包含:
import wpf
from System.Windows import MessageBox
from System.Windows import Application, Window
class MyWindow(Window):
def __init__(self):
self.str1 = ""
wpf.LoadComponent(self, 'IronPython5.xaml')
def Button_Click(self, sender, e):
if self.str1 == "":
MessageBox.Show("msg1")
else:
MessageBox.Show("msg2")
pass
if __name__ == '__main__':
Application().Run(MyWindow())
Run Code Online (Sandbox Code Playgroud)
IronPython5.py 还包含:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="IronPython5" Height="300" Width="300">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="159,238,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
dll 文件是:
请帮助我如何修复此错误以生成 exe 文件。
我刚刚按照此解决方案给出的一些提示运行了示例 WPF IronPython 项目给出的一些提示运行了示例 WPF IronPython 项目,但我使用了 IronPython 2.7.7 安装附带的 ipyc.exe 来编译它。
为了在最终的可执行文件中包含所有依赖项,您似乎必须手动加载它们。
请注意,如果没有 try/ except 块,这将无法在 Visual Studio 中运行,因为 VS 可能已经添加了这些块
import clr
try:
clr.AddReferenceToFileAndPath("IronPython.Wpf.dll")
clr.AddReferenceToFileAndPath('PresentationCore.dll')
clr.AddReferenceToFileAndPath('PresentationFramework.dll')
clr.AddReferenceToFileAndPath('WindowsBase.dll')
except:
pass
from System.Windows import Application, Window
import wpf
class MyWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'Wpf_Example.xaml')
if __name__ == '__main__':
Application().Run(MyWindow())
Run Code Online (Sandbox Code Playgroud)
然后将 dll 添加到您的项目文件夹中。它们可以在这里找到:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\
并用ipyc编译
ipyc Wpf_Example.py /target:winexe
Run Code Online (Sandbox Code Playgroud)
注意调用 ipyc 的不同方式!如果您使用 /main: 选项,它将不起作用!