Zoe Plaxco
Zoe Plaxco

Reputation: 60

wx.DirDialog not closing

Using wxPython version 4.0.1 (pheonix) with python 3.6.5

I use a wxPython DirDialog to allow my user to input a start directory. It correctly selects and populates my "working directory" variable (using GetPath()) but then doesn't ever close the directory dialog prompt box.

I read through the wxPython-user google pages and the only related question I found referred to this as being "intended behavior," implying it would happen later in execution (https://groups.google.com/forum/#!searchin/wxpython-users/close%7Csort:date/wxpython-users/ysEZK5PVBN4/ieLGEWc6AQAJ).

Mine, however, doesn't close until the entire script has completed running (which takes a fair amount of time), giving me the spinning wheel of death. I have tried a combination of calls to try to force the window to close.

app = wx.App()
openFileDialog = wx.DirDialog(None, "Select", curr, wx.DD_DIR_MUST_EXIST)
openFileDialog.ShowModal()
working_directory = openFileDialog.GetPath()
openFileDialog.EndModal(wx.CANCEL) #also wx.Close(True) and wx.Destroy()
openFileDialog.Destroy()
openFileDialog=None

I have also tried creating a window, passing it as the parent of the DirDialog, and then closing the window and it gives the same behavior.

Upvotes: 0

Views: 286

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

You don't mention which operating system you are on or version of wx but in the partial code that you supplied there is no MainLoop, which is what was mentioned by Robin Dunn in his answer, in your link.
Try this and see if it works the way you would expect.

import wx
from os.path import expanduser
import time

class choose(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Dialog")
        panel = wx.Panel(self,-1)
        text = wx.StaticText(panel,-1, "Place holder for chosen directory")
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Show()
        curr = expanduser("~")
        dlg = wx.DirDialog(None, message="Choose a directory", defaultPath = curr,
          style=wx.DD_DEFAULT_STYLE|wx.DD_DIR_MUST_EXIST)
        if dlg.ShowModal() == wx.ID_OK:
            text.SetLabel(dlg.GetPath())
        dlg.Destroy()
        b = wx.BusyInfo("I'm busy counting",parent=None)
        wx.Yield()
        for i in range(30):
            time.sleep(1)
        del b


    def OnClose(self, event):
        self.Destroy()

if __name__ == '__main__':
    my_app = wx.App()
    choose(None)
    my_app.MainLoop()

Upvotes: 1

Related Questions