inconsistent behavior with Anaconda Python of wxPython dialogs

418
January 06, 2017, at 12:13 PM

I'm seeing some odd behavior with my wxPython application. If I try to open a DirDialog during before the first wx.Frame.init is over, the DirDialog opens but closes immediately, returning wx.ID_CANCEL. Then, if I open a second DirDialog, it works as normal.

I get this error only with my Anaconda distribution of Python. I don't get it with my Canopy Python distribution or my brew-installed Python.

Here is the code:

class simpleapp_wx(wx.Frame):
    def __init__(self, parent, title='my application'):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(300, 300))
        self.parent = parent
        self.initialize()
        print 'first try'
        self.choose_directory(None) # closes immediately if using Anaconda
        print 'second try'
        self.choose_directory(None) 
        self.SetSizerAndFit(self.sizer)
        self.Centre()
        self.Show(True)
    def initialize(self):
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        wx_version = wx.StaticText(self, -1, label=str(wx.__version__))
        self.sizer.Add(wx_version, flag=wx.ALL, border=10)
        button = wx.Button(self, -1, label="Click me!")
        self.sizer.Add(button, flag=wx.ALL, border=10)
        self.Bind(wx.EVT_BUTTON, self.choose_directory, button)
        self.label = wx.StaticText(self, -1, label=u'Hello !')
        self.sizer.Add(self.label, flag=wx.ALL, border=10)
    def choose_directory(self, event):
        dlg = wx.DirDialog(self, message="please choose a directory")
        res = dlg.ShowModal()
        if res == wx.ID_OK:
            new_dir = dlg.GetPath()
            dlg.Destroy()
            self.label.SetLabel(new_dir)
            self.SetSizerAndFit(self.sizer)
            return new_dir
        else:
            dlg.Destroy()
            self.label.SetLabel(os.getcwd())
            self.SetSizerAndFit(self.sizer)
            return "."

When I run the above code with Anaconda, I see one DirDialog. When I run it with any other Python, I see two DirDialogs.

I have tried different versions of the wx package and different versions of Python, and I get the odd behavior with all Anaconda permutations and the expected behavior with all non-Anaconda permutations.

I have a hack-y solution that produces the correct behavior with Anaconda or non-Anaconda Python:

self.initialize()
self.choose_directory(None)
if 'Anaconda' in sys.version.split()[1]:
    self.choose_directory(None)

Anyone have a better solution, or an explanation of this strange behavior?

READ ALSO
How do I make sense of this diagram from Google's deep learning course?

How do I make sense of this diagram from Google's deep learning course?

I have been enjoying Google's deep learning course while finding it relatively difficultI am still in the first section which is going through multinomial logistic classification, specifically in regards to classifying pictures of letters according to what letter...

350
Music Y or Music N

Music Y or Music N

In python, "Music on or music off"If user selects "Y" music will play, If user selects "N" I don't want music to play etc

335
pytest/celery - Failed: Database access not allowed

pytest/celery - Failed: Database access not allowed

I'm having some trouble getting pytest to test a celery task that's part of a django projectThe only way I can get it to work is to import the task in the test method

593
how to combine element in tuple or list accordingly python

how to combine element in tuple or list accordingly python

I have a few tuples look like thisI would like to combine all the word in the same sentence

486