pythonnet / pythonnet/pythonnet
Event handling with WPF application
Personne n'a encore pris cette issue.
- Langage dominant
- C#
- Étoiles
- 5.5k
- Forks
- 780
- Métriques de merge des PR
- Aucune PR mergée en 30 j
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par les fichiers de démonstration DynamicGrid.xaml et DynamicGrid.py, en particulier XamlReader.Load et l’abonnement existant à BTN.Click. Comparez le comportement du gestionnaire XAML avec celui du gestionnaire d’exécution fonctionnel et avec le comportement décrit d’IronPython. Le travail sera considéré comme terminé lorsqu’il existera une méthode documentée ou démontrée permettant à un événement de bouton WPF déclaré dans XAML d’appeler du code Python via pythonnet.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- desktop
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100