pythonnet / pythonnet/pythonnet

Event handling with WPF application

未关闭
#730 3 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
C#
星标
5.5k
派生
780
PR 合并指标
30 天内没有已合并 PR

描述

Environment
  • Pythonnet version: 2.3.0
  • Python version: 3.6 (x64)
  • Operating System: Windows 10
  • IDE: Visual Studio 2017
Details
  • I would like to build a WPF application using python 3.6. My starting point is the DynamicGrid.py demo, and so far this works fine.

  • Now I would like to handle the control events in my python code. For this I replaced one of the labels in the DynamicGrid.xaml file with a button and added a Click event handler:

<Button Name="BTN" Content="Left" Grid.Column="0" Background="LightBlue" Click="btn_click"/>

The name of this event handler is also added to the corresponding python file DynamicGrid.py:

class MyWindow(Window):
    def __init__(self):
        stream = StreamReader("DynamicGrid.xaml")
        window = XamlReader.Load(stream.BaseStream)
        Application().Run(window)
     
    def btn_click(self, sender, e):
        print("Button clicked!")

Code like this is created when using the IronPython WPF Project in Visual Studio 2017 and works well with IPython. However, when incorporating event handlers like this with pythonnet I get the following Python.Runtime.PythonException:

XamlParseException : Line 16 ... "Error Creating "Click" from Text "btn_click".

It is however possible to add an event handler at runtime.

  • For this to work the Click-Event handler has to be removed from the XAML definition:
    <Button Name="BTN" Content="Left" Grid.Column="0" Background="LightBlue"/>

  • In the init code, search for the button's name and add the event handler:

        BTN = window.FindName('BTN')
        BTN.Click += self.btn_click

While this works it seems somewhat tedious to do this for larger, complex controls and UIs. Is there a way to declare the event handlers in the XAML file, just like in IronPython? If so, it might be helpful to others to include such a declaration in the DynamicGrid Demo.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 DynamicGrid.xaml 和 DynamicGrid.py 演示文件开始,重点查看 XamlReader.Load 和现有的 BTN.Click 订阅。将 XAML 处理程序的行为与可正常工作的运行时处理程序以及所描述的 IronPython 行为进行比较。完成的标准是:有文档说明或演示一种方式,使 XAML 中声明的 WPF 按钮事件能够通过 pythonnet 调用 Python 代码。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
desktop
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。