tags:

views:

39

answers:

1

Hi all, my problem stems from the use of wxApp as far as I can tell.

Inside a litte subroutine I call a wx.MessageDialog to ask for a yes or no. I retrieve the answer an process some stuff acordingly. My example code below actually works (though it might be ugly) except that the Dialog box stays open after hitting one of the buttons...

import wx
from os import path
def HasFile(filename):
    if path.isfile(filename ):
        app = wx.App()
        dial = wx.MessageDialog(None, 
                                'Die Datei "'+filename+'" wurde gefunden.\nSoll sie eingelesen werden?',
                                'Bitte antworten',
                                wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
        result = dial.ShowModal()
        dial.EndModal(retCode=0)
        dial.Destroy()
        app.MainLoop()
        return result == wx.ID_YES
    else:
        return False

So while the rest of the program does whatever is expected, the box just sits there. It also seems like my calls to matplotlib later on are producing errors (a Tkinter error to be precise), maybe because of the wx stuff?

Edit: I tried to end the app with a call to app.Destroy(). This doesn't change the fact that the box is still there. When I issue a app.IsActive() afterwards the whole program exits (almost like a sys.exit())! How come?

Edit 2: Adding a wxApp like this isn't a good idea since the other scripts get affected as well. Subsequent plotting commands don't get displayed and I don't know how to fix this. Thus I decided to remove the DialogBox alltogether.

+1  A: 

Calling your function with EndModal removed, it works okay and returns me to the console after choosing yes/no. Selecting one of those basically calls EndModal, and you calling that manually was throwing an exception

Traceback (most recent call last):
  File "blah.py", line 19, in <module>
    HasFile("C:\tbzrcommand_args.txt")
  File "blah.py", line 12, in HasFile
    dial.EndModal(retCode=0)
  File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_windows.py", line 715, in EndModal
    return _windows_.Dialog_EndModal(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "IsModal()" failed at ..\..\src\msw\dialog.cpp(361) in wxDialog::EndModal(): EndModal() called for non modal dialog

fix:

import wx
from os import path
def HasFile(filename):
    if path.isfile(filename ):
        print 'gfd'
        app = wx.App(redirect=False)
        dial = wx.MessageDialog(None,
                                'Die Datei "'+filename+'" wurde gefunden.\nSoll sie eingelesen werden?',
                                'Bitte antworten',
                                wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
        result = dial.ShowModal()
        dial.Destroy()
        app.MainLoop()
        return result == wx.ID_YES
    else:
        return False
Steven Sproat