python - Pythonnet 错误:XamlParseException:“无法从文本中创建“点击”
问题描述
我之前在 IronPython 上工作(使用 WPF 开发一些 GUI),最近我开始尝试 pythonnet。
但是我发现在 IronPython 上工作的 xaml 文件在 CPython + pythonnet 上不起作用。在 IronPython 中,我可以在 xaml 文件中定义一个 Button.Click,但在 CPython 中似乎不可能。我试图寻找答案,但没有找到相关的。所以希望你能在这里救我...
这是我的主要脚本:
import clr
clr.AddReference(r"wpf\PresentationFramework")
from System.IO import StreamReader
from System.Windows.Markup import XamlReader
from System.Windows import Application, Window
from System.Threading import Thread, ThreadStart, ApartmentState
class MyWindow(Window):
def __init__(self):
stream = StreamReader('test.xaml')
window = XamlReader.Load(stream.BaseStream)
Application().Run(window)
def Button_Click(self, sender, e):
print('Button has clicked')
if __name__ == '__main__':
thread = Thread(ThreadStart(MyWindow))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
这是test.xmal:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WpfApplication1" Height="300" Width="300">
<Grid>
<Button x:Name="BUTTON" Content="Button" HorizontalAlignment="Left" Margin="101,82,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" Background="#FFFF1616"/>
</Grid>
</Window>
我得到的错误信息是:
未处理的异常:Python.Runtime.PythonException:XamlParseException:“无法从文本“Button_Click”创建“单击”。行号“6”和行位置“132”。
奇怪的是,如果我在 IronPython 中加载相同的 xaml 并保持相同的类结构,脚本就可以正常工作:
import wpf
from System.Windows import Application, Window
class MyWindow(Window):
def __init__(self):
self.ui = wpf.LoadComponent(self, 'test.xaml')
def Button_Click(self, sender, e):
print('Button has clicked')
if __name__ == '__main__':
Application().Run(MyWindow())
非常感谢您的帮助!
解决方案
您不能使用 pythonnet 包在 python 中导入 wpf。但是您可以使用 PresentationFramework 加载 XAML 文件,在presentationframework 中存在一些限制。XamlReader 无法执行事件处理,因此我们可以在 python 文件中手动添加该按钮的事件,而不是在 XAML 文件中。
听 gui.xaml 代码
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WpfApplication1" Height="300" Width="300">
<Grid>
<Label Content="Hello world" HorizontalAlignment="Left" Height="32" Margin="65,77,0,0" VerticalAlignment="Top" Width="119"/>
<Button Name="button1" Content="Button" HorizontalAlignment="Left" Margin="65,145,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
然后听python代码
import clr
clr.AddReference("wpf\PresentationFramework")
from System.IO import *
from System.Windows.Markup import XamlReader
from System.Windows import *
from System.Threading import Thread, ThreadStart, ApartmentState
from System.Windows.Controls import *
class MyWindow(Window):
def __init__(self):
try:
stream = StreamReader('gui.xaml')
self.window = XamlReader.Load(stream.BaseStream)
ButtoninXAML = LogicalTreeHelper.FindLogicalNode(self.window, "button1")
ButtoninXAML.Click += RoutedEventHandler(self.Button_Click)
Application().Run(self.window)
except Exception as ex:
print(ex)
def Button_Click(self, sender, e):
print('clicked')
if __name__ == '__main__':
thread = Thread(ThreadStart(MyWindow))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
推荐阅读
- sql-server - SQL Server 存储过程中的死锁
- regex - IIS 重写规则 - 忽略 localhost
- android - 如何在设置 PreferenceScreen Android 中调整矢量图标的大小
- java-8 - DoubleSummaryStatistics 中的 sum Vs simpleSum vs sumCompensation?拥有它们有什么意义?
- python - 提供与 Python 3.6 变量注释的向后兼容性
- dataframe - 数据框拆分查询
- angular - Angular:为什么排序只适用于第一个 Mat Table?
- php - 如何从 PHP 文件中提取 JSON 数据?
- python - 如何检测我的套接字何时失去其 TCP 连接并重新连接?
- php - PHPMailer 在我的家用电脑上工作,mamp 在本地,不能在工作电脑上工作,总是在本地使用 mamp