2

I'm trying to create a simple GUI using WPF in VS2017. I've tried following code to show a MessageBox after clicking generate button:

import wpf
from System.Windows import MessageBox
def generate_Btn_Click(self, sender, e):
   MessageBox.Show("hi")

and it will show up if I click on the generate button but, if I try to add an if statement before MessageBox.Show():

def generate_Btn_Click(self, sender, e):
    if A == None: 
        MessageBox.Show("Message1!")
    else:
        MessageBox.Show("Message2!")

after clicking on generate button, no MessageBox appears and the window closes. My question is how to use MessageBox In IronPython using WPF. A simple example that will not work when I push the button is as follows:

import wpf
from System.Windows import MessageBox
from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'IronPython5.xaml')
    str1 = ""
    def Button_Click(self, sender, e):
        if str1 == "":
            MessageBox.Show("msg1")
        else:
            MessageBox.Show("msg2")

        pass


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

This is the XAML:

<Window 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="IronPython5" Height="300" Width="300"> 
       <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="159,238,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window> 
8
  • if A == None: the A may throw an error unless it refers to something in your code ? Commented Jul 24, 2017 at 6:00
  • @PRMoureu it refers to a variable and is valid. I deleted extra codes to make it clean. I use A in other parts of code and is ok. Commented Jul 24, 2017 at 6:42
  • is A a global variable? Commented Jul 24, 2017 at 22:48
  • @denfromufa it's a member of class defined for current project and is initialized to None Commented Jul 25, 2017 at 3:57
  • 1
    then you need to show your method within the class and how it is instantiated. currently your code would not run and is not reproducible. i bet if you start your app from cmd, then some error would show up in the console for the second case. Commented Jul 25, 2017 at 4:19

1 Answer 1

2

When I ran from cmd, here is the error I got:

C:\Program Files (x86)\IronPython 2.7>ipy wpfapp.py
Traceback (most recent call last):
  File "wpfapp.py", line 19, in <module>
  File "wpfapp.py", line 10, in Button_Click
NameError: global name 'str1' is not defined

You cannot define global variables inside class!

And now here is a quick fix for you:

import wpf
from System.Windows import MessageBox
from System.Windows import Application, Window

class MyWindow(Window):
    def __init__(self):
        self.str1 = ""
        wpf.LoadComponent(self, 'app.xaml')
    def Button_Click(self, sender, e):
        if self.str1 == "":
            MessageBox.Show("msg1")
        else:
            MessageBox.Show("msg2")

        pass


if __name__ == '__main__':
    Application().Run(MyWindow())
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. It works fine! please let me know how did you started the code using ipy.exe? is it actually necessary to put both XAML and python code in ipy.exe directory and then run it? I don't know how to run the IronPython projects created by VS using the commad line.

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.