1

Im new to python and VS and im trying to make a simple GUI with a button. Once I click the button I want it to print(5).

The code looks as following but when I click "run" it exits without any action:

import wpf

from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'WpfApplication1.xaml')

    BUTTON.Click += self.Button_Click
    print(5)

def Button_Click(self, sender, e):
    pass

if __name__ == '__main__':
    Application().Run(MyWindow())

XAML:

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="WpfApplication1" Height="300" Width="300"> 
       <Grid>
        <Button x:Name="BUTTON" Content="Button" HorizontalAlignment="Left" Margin="101,82,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" Background="#FFFF1616"/>
    </Grid>
</Window> 

Thank you.

3
  • Would be easier to discuss this if you could edit your answer to include the relevant portion of the xaml code? Commented Dec 23, 2017 at 15:53
  • added the XAML. Commented Dec 23, 2017 at 16:07
  • Also, If I may suggest a title edit, something like this would be more appropriate since your question is about ironpython/wpf and not Visual Studio: Button Click Event on IronPython + Wpf Commented Dec 23, 2017 at 17:48

1 Answer 1

2

You have to add an event handler for the button click. Just add this to your window init. (BUTTON should match the button's name on your xaml code)

ui = wpf.LoadComponent(self, 'WpfApplication1.xaml')
ui.BUTTON.Click += self.Button_Click

You can also achieve the same through the xaml code :

 <Button x:Name="BUTTON" Click="Button_Click"></Button>

Working code with comments below:

import wpf

from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        self.ui = wpf.LoadComponent(self, 'form.xaml')
        # not needed because event handler
        # is in XAML
        # to handle event on code, remove this from xaml's button tag:
        # Click="Button_Click"
        # and uncomment line below:
        # self.ui.Button.Click += self.Button_Click

    def Button_Click(self, sender, e):
        print('Button has clicked')

if __name__ == '__main__':
    Application().Run(MyWindow())
    # Alternatively, below also works:
    # form = MyWindow()
    # form.ShowDialog()

See screenshot of working form: enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I changed it but it didnt work, I updated the question mate.
it seems like after 1 compile the program stops compiling no matter what the code is inside and I get the following error: The program '[3484] ipyw.exe' has exited with code 1 (0x1).
Thank you for the answer. Can you please tell me what IDE you use? Because in Visual studio I cant see any output from this code (at least I dont get any error tho)
Happy to help. Not really using an IDE. It's just sublime editor, and I execute the code on a command prompt/terminal using ipy.exe filename.py (ipy.exe has to be added to system PATH)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.