pythonnet / pythonnet/pythonnet

Event handling with WPF application

Open
#730 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
5.5k
Forks
780
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DynamicGrid.xaml and DynamicGrid.py demo files, especially XamlReader.Load and the existing BTN.Click subscription. Compare the XAML handler behavior with the working runtime handler and the described IronPython behavior. Done would mean a documented or demonstrated way for a WPF button event declared in XAML to invoke Python code through pythonnet.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
desktop
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.