Beginner Question: WPFApplication and Python

Mar 13, 2015 at 5:17 PM
Edited Mar 13, 2015 at 5:17 PM
Hello,
I 'm currently going to teach me myself Python. Therefore I would like to use Visual Studio 2013. I have already installed Python Tools with Python 3.4 and in Addition IronPython.

With Pyhton 3.4 und just using the stanard editor (without a gui) I have already taken first steps successfully .

So now I would like to combine my Programm with a GUI created by WPF (a XAML file). Getting actions done by double-clicking a button.


My Problem:

In my GUI I have a label and a button. I would like when you click on the button that the Label1 changes to the text I defined in the button action. For example from " Test1 " in " Test2 " .

The error message tells that label1 is not defined?!

How do I get a connection between XAML and PY ?

I hope you understand my problem...


XAML:
<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
       Title="WpfApplication2" Height="219.403" Width="419.403">
    <Grid>
        <Button x:Name="button1" Content="Press for Change!" HorizontalAlignment="Left" Margin="151,117,0,0" VerticalAlignment="Top" Width="102" Click="Button_Click"/>
        <Label x:Name="label1" Content="Test1" HorizontalAlignment="Left" Margin="103,46,0,0" VerticalAlignment="Top" Height="29" Width="119"/>
    </Grid>
</Window> 
PYTHON:
import wpf

from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'WpfApplication2.xaml')
    
    def Button_Click(self, sender, e):
        label1.configure(text="Test2")

if __name__ == '__main__':
    Application().Run(MyWindow())
Coordinator
Mar 13, 2015 at 7:07 PM
Could you try 'self.label1' instead of 'label1'?
Mar 15, 2015 at 7:51 PM
I tried this, but the the programm stops by 'Label' object has no attribute 'configure' ?
Coordinator
Mar 15, 2015 at 9:51 PM
Edited Mar 15, 2015 at 9:54 PM
I'm not sure where configure is even supposed to come from. In WPF, a label simply has a property named Text, and you access it as an attribute:
self.label1.Text = 'Test2'
You can see all members of the class on MSDN, e.g. here is the doc page for Label. For the most part, projection of .NET API surface to Python is straightforward - properties are properties and methods are methods - but IronPython also has some docs covering the more advanced cases, like generics or overloaded methods.