I\'m teaching an introductory class to programming and GUI development using Python, and have found that the least overwhelming solution for students new to programming is t
In IronPython 2.7 the wpf.LoadComponent method will wire up any properties with the same name as the XAML UI elements. If you are using IronPython 2.6 then you would need to use the code as suggested by WombatPM. So with IronPython 2.7 if you use the following XAML:
Then you can define two properties called button and textbox to access the UI elements:
class MyWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'IronPyWpf.xaml')
self._button.Content = 'My Button'
self._textbox.Text = 'My Text'
def get_button(self):
return self._button
def set_button(self, value):
self._button = value
button = property(get_button, set_button)
def get_textbox(self):
return self._textbox
def set_textbox(self, value):
self._textbox = value
textbox = property(get_textbox, set_textbox)
In fact it seems that you can simplify the code even further by removing the property definitions:
class MyWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'IronPyWpf.xaml')
self.button.Content = 'My Button'
self.textbox.Text = 'My Text'
Unfortunately Visual Studio seems to crash, as you have already seen, with a null reference exception when you try to edit the XAML and give the UI elements a name.